Repeater嵌套经常要用到,总结一下。
<asp:Repeater ID="repSingle" runat="server" onitemcommand="repSingle_ItemCommand" onitemdatabound="repSingle_ItemDataBound"> <ItemTemplate> <li>序号<asp:TextBox ID="texTrueFalseSequence" runat="server" Width="20px" Text='<%#Eval("Sequence")%>'></asp:TextBox> <%#Eval("QuestionTitle")%></li> <!--begin选项--> <li> <ul> <asp:Repeater ID="repSingleChoices" runat="server"> <ItemTemplate> <li><%# Container.ItemIndex + 1%>,<%#Eval("ItemContent")%></li> </ItemTemplate> </asp:Repeater> </ul> </li> <!--end选项--> </ItemTemplate> </asp:Repeater>
protected void repSingle_ItemDataBound(object sender, RepeaterItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater singleChoice = e.Item.FindControl("repSingleChoices") as Repeater; Question row = (Question)e.Item.DataItem; //int curSequence = Convert.ToInt32(row["Sequence"].ToString()); //Question curQuestion = questionnaire.Questions.Find(a => (a.QuestionType == QuestionType.singlechoice && a.Sequence == curSequence)); singleChoice.DataSource = row.ChoiceItems; singleChoice.DataBind(); } }
发现一个现象,即使没有设置AlternatingItem,默认仍然会在第一行是ListItemType.Item,第二行是ListItemType.AlternatingItem,如此交替下去