Developer
Developer

Reputation: 53

Find Selected Checkboxes in Repeater

I have this code and need to know which checkboxes are selected in code behind

Can anyone help me ?

<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>

Upvotes: 1

Views: 6109

Answers (2)

Developer
Developer

Reputation: 53

this is the last try to solve and all checkbox.Checked not change all equal false

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>

Upvotes: 0

Curtis
Curtis

Reputation: 103348

  • Loop through your ParentRepeater items
  • Find the child repeater in each item, and loop through thats items
  • Find the checkbox and check whether its checked
  • If its checked, Response.Write ltlStudentName in same item
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

And change one line of your markup to:

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

Upvotes: 1

Related Questions