在Repeater中查找选定的复选框

时间:2012-03-31 01:28:34

标签: asp.net

我有这段代码,需要知道在

背后的代码中选择了哪些复选框

任何人都可以帮助我吗?

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
            <ItemTemplate>
                <label>
                    <%# Eval("TeacherName")%>
                </label>
                <br />
                <asp:Repeater ID="ChildRepeater" runat="server">
                    <ItemTemplate>
                        <label>
                            <input type="checkbox" id="students" runat="server" /><%# Eval("StudentName")%>
                        </label>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

2 个答案:

答案 0 :(得分:1)

  • 循环浏览ParentRepeater项目
  • 在每个项目中查找子转发器,并循环显示该项目
  • 找到复选框并检查其是否已选中
  • 如果已选中,则相同项目中的Response.Write ltlStudentName
For each item as repeateritem in ParentRepeater.Items
    For each item2 as repeateritem in ctype(item.findcontrol("ChildRepeater"),repeater).items
      if ctype(item2.findcontrol("students"),checkbox)
         response.write(ctype("ltlStudentName"),literal).text)
      end if
    next
Next

将标记的一行更改为:

<input type="checkbox" id="foods" runat="server" /><asp:literal id="ltlStudentName" runat="server" text='<%# Eval("StudentName")%>' />

答案 1 :(得分:0)

这是最后一次尝试解决和所有checkbox.Checked不会改变所有相等的错误

foreach (RepeaterItem item in ParentRepeater.Items)
            {
                Repeater rep = ((Repeater)item.FindControl("ChildRepeater"));

                foreach (RepeaterItem item2 in rep.Items)
                {
                    var chkBox = item2.FindControl("students") as HtmlInputCheckBox;

                }
            }


<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
            <ItemTemplate>
                <label>
                    <%# Eval("TeacherName")%>
                </label>
                <br />
                <asp:Repeater ID="ChildRepeater" runat="server">
                    <ItemTemplate>
                        <label>
                           <input type="checkbox" id="students" runat="server" /><asp:literal id="ltlStudentName" runat="server" text='<%# Eval("StudentName")%>' />
                        </label>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>