Search code examples
asp.netwebformscheckboxlist

Get checked checkbox list values inside a repeater


How can I get checked values form checkboxlists inside a repeater, I have no idea how collect all checked values in a collection list.

<asp:Repeater runat="server" ID="rp_outer" OnItemDataBound="rp_outer_ItemDataBound">
                 <ItemTemplate>
                     <a class="collapsed btn" data-toggle="collapse" data-target="#Col<%#Eval("ProID") %>">  <%#Eval("PropEName") %></a>

                     <div id="Col<%#Eval("ProID") %>" class="collapse in">

                         <asp:CheckBoxList ID="cbxlist" runat="server" CssClass="filter-ul" DataSource='<%# DataBinder.Eval(Container.DataItem, "rltbls") %>' DataValueField='ID' DataTextField='ValuesEName'>

                         </asp:CheckBoxList>


                     </div>
                     <hr />


                 </ItemTemplate>
             </asp:Repeater>

enter image description here


Solution

  • you need grouping items with ProId.

         <ItemTemplate>
               <a class="collapsed btn" data-toggle="collapse" data-target="#Col<%#Eval("ProID") %>">  <%#Eval("PropEName") %></a>
               <asp:HiddenField runat="server" ID="ProID" Value="<%#Eval("ProID") %>"/>
               <div id="Col<%#Eval("ProID") %>" class="collapse in">
    ....
    

    You can create a dictionary list that uses ProId as a key

        private Dictionary<string, List<string>> GetCheckedItems()
        {
            Dictionary<string, List<string>> checkedItemsList = new Dictionary<string, List<string>>();
            foreach (RepeaterItem item in rp_outer.Items)
                if (item.ItemType == ListItemType.Item)
                {
                    CheckBoxList itemCheckBoxList = item.FindControl("cbxlist") as CheckBoxList;
                    if (itemCheckBoxList != null)
                    {
                        string proIdValue = (item.FindControl("ProID") as HiddenField).Value;
                        List<string> checkedItems = new List<string>();
                        foreach (ListItem checkBoxItem in itemCheckBoxList.Items)
                            if (checkBoxItem.Selected)
                                checkedItems.Add(checkBoxItem.Value);
                        checkedItemsList.Add(proIdValue, checkedItems);
                    }
                }
            return checkedItemsList;
        }