[Repeater]回一个兄弟的提问
1 namespace WebApp 2 { 3 class School 4 { 5 public string Name 6 { 7 get; 8 set; 9 } 10 11 public List<Grade> grades; 12 } 13 class Grade 14 { 15 public string Name 16 { 17 get; 18 set; 19 } 20 21 public List<Class> classes; 22 } 23 class Class 24 { 25 public string Name 26 { 27 get; 28 set; 29 } 30 } 31 public partial class _Default : System.Web.UI.Page 32 { 33 protected void Page_Load(object sender, EventArgs e) 34 { 35 List<School> schools=new List<School>(); 36 37 //高中 38 School s1 = new School(); 39 s1.Name = "高中"; 40 s1.grades = new List<Grade>(); 41 42 //高三 43 Grade g11 = new Grade(); 44 g11.Name = "2001级 三年级"; 45 g11.classes = new List<Class>(); 46 47 Class c111 = new Class(); 48 c111.Name = "1班"; 49 g11.classes.Add(c111); 50 51 Class c112 = new Class(); 52 c112.Name = "2班"; 53 g11.classes.Add(c112); 54 55 Class c113= new Class(); 56 c113.Name = "3班"; 57 g11.classes.Add(c113); 58 59 Class c114 = new Class(); 60 c114.Name = "4班"; 61 g11.classes.Add(c114); 62 63 Class c115 = new Class(); 64 c115.Name = "5班"; 65 g11.classes.Add(c115); 66 67 Class c116 = new Class(); 68 c116.Name = "6班"; 69 g11.classes.Add(c116); 70 71 s1.grades.Add(g11); 72 73 //高二 74 Grade g12 = new Grade(); 75 g12.Name = "2002级 二年级"; 76 g12.classes = new List<Class>(); 77 78 Class c121 = new Class(); 79 c121.Name = "1班"; 80 g12.classes.Add(c121); 81 82 Class c122 = new Class(); 83 c122.Name = "2班"; 84 g12.classes.Add(c122); 85 86 Class c123 = new Class(); 87 c123.Name = "3班"; 88 g12.classes.Add(c123); 89 90 Class c124 = new Class(); 91 c124.Name = "4班"; 92 g12.classes.Add(c124); 93 94 Class c125 = new Class(); 95 c125.Name = "5班"; 96 g12.classes.Add(c125); 97 98 s1.grades.Add(g12); 99 100 //高一 101 Grade g13 = new Grade(); 102 g13.Name = "2002级 二年级"; 103 g13.classes = new List<Class>(); 104 105 Class c131 = new Class(); 106 c131.Name = "1班"; 107 g13.classes.Add(c131); 108 109 Class c132 = new Class(); 110 c132.Name = "2班"; 111 g13.classes.Add(c132); 112 113 Class c133 = new Class(); 114 c133.Name = "3班"; 115 g13.classes.Add(c133); 116 117 Class c134 = new Class(); 118 c134.Name = "4班"; 119 g13.classes.Add(c134); 120 121 Class c135 = new Class(); 122 c135.Name = "5班"; 123 g13.classes.Add(c135); 124 125 Class c136 = new Class(); 126 c136.Name = "6班"; 127 g13.classes.Add(c136); 128 129 Class c137 = new Class(); 130 c137.Name = "7班"; 131 g13.classes.Add(c137); 132 133 Class c138 = new Class(); 134 c138.Name = "8班"; 135 g13.classes.Add(c138); 136 137 s1.grades.Add(g13); 138 139 schools.Add(s1); 140 141 //初中 142 School s2 = new School(); 143 s2.Name = "初中"; 144 s2.grades = new List<Grade>(); 145 146 //初四 147 Grade g21 = new Grade(); 148 g21.Name = "2004级 四年级"; 149 g21.classes = new List<Class>(); 150 151 Class c211 = new Class(); 152 c211.Name = "1班"; 153 g21.classes.Add(c211); 154 155 Class c212 = new Class(); 156 c212.Name = "2班"; 157 g21.classes.Add(c212); 158 159 Class c213 = new Class(); 160 c213.Name = "3班"; 161 g21.classes.Add(c213); 162 163 Class c214 = new Class(); 164 c214.Name = "4班"; 165 g21.classes.Add(c214); 166 167 Class c215 = new Class(); 168 c215.Name = "5班"; 169 g21.classes.Add(c215); 170 171 Class c216 = new Class(); 172 c216.Name = "6班"; 173 g21.classes.Add(c216); 174 175 s2.grades.Add(g21); 176 177 schools.Add(s2); 178 179 180 rptSchool.DataSource = schools; 181 rptSchool.DataBind(); 182 183 } 184 185 //绑定初高中,大学,研究生...神马时处理... 186 protected void rptSchool_ItemDataBound(object sender, RepeaterItemEventArgs e) 187 { 188 //<ItemTemplate> || <AlternatingItemTemplate> 189 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 190 { 191 Repeater rpt = e.Item.FindControl("rptGrade") as Repeater; 192 School s = (School)e.Item.DataItem; 193 194 rpt.DataSource = s.grades; 195 rpt.DataBind(); 196 } 197 } 198 199 //绑定一年级,二年级...神马时处理... 200 protected void rptGrade_ItemDataBound(object sender, RepeaterItemEventArgs e) 201 { 202 //<ItemTemplate> || <AlternatingItemTemplate> 203 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 204 { 205 Repeater rpt = e.Item.FindControl("rptClass") as Repeater; 206 Grade g = (Grade)e.Item.DataItem; 207 208 rpt.DataSource = g.classes; 209 rpt.DataBind(); 210 } 211 } 212 } 213 }
1 <asp:Repeater runat="server" ID="rptSchool" OnItemDataBound="rptSchool_ItemDataBound"> 2 <HeaderTemplate> 3 <div id="Demo"> 4 </HeaderTemplate> 5 <ItemTemplate> 6 <div class="school"> 7 <h2><%# Eval("Name") %></h2> 8 <asp:Repeater runat="server" ID="rptGrade" OnItemDataBound="rptGrade_ItemDataBound"> 9 <HeaderTemplate> 10 <ul class="grade"> 11 </HeaderTemplate> 12 <ItemTemplate> 13 <li><span class="gname"><%# Eval("Name") %></span><asp:Repeater runat="server" ID="rptClass"><ItemTemplate><a class="cls"><%# Eval("Name") %></a></ItemTemplate></asp:Repeater></li> 14 </ItemTemplate> 15 <FooterTemplate> 16 </ul> 17 </FooterTemplate> 18 </asp:Repeater> 19 </div> 20 </ItemTemplate> 21 <FooterTemplate> 22 </div> 23 </FooterTemplate> 24 </asp:Repeater>
1 <style type="text/css"> 2 ul,li 3 { 4 margin:0; 5 padding:0; 6 list-style:none; 7 } 8 .school h2 9 { 10 font-size:14px; 11 color:#C00; 12 } 13 .grade li 14 { 15 margin:4px 0; 16 } 17 .gname 18 { 19 color:#960; 20 line-height:24px; 21 background-color:#FFEECC; 22 display:inline-block; 23 padding:0 5px; 24 } 25 26 .grade li a 27 { 28 display:inline-block; 29 background-color:#f0f0f0; 30 margin:0 5px; 31 line-height:24px; 32 padding:0 5px; 33 } 34 </style>
注:代码随手一写,不要直接用于生产环境;-)