locked
Asp.net: Get ID of checked CheckBoxes in a ListView RRS feed

  • Question

  • User-380220413 posted

    Asp.net, Webforms, c# 4.8: I have a page with a ListView control that contains checkboxes. In the postback, I need to loop through the list and get the IDs of all the checkboxes that were checked, but I dont know how to assign the ID to the checkboxes when the page is generated, and in the postback how to get the IDs from the checked checkboxes. Can someone please explain?

    Here's my html:

    <asp:ListView ID="CategoryCheckList" runat="server" class="CListStyle"
         GroupItemCount="3" ItemPlaceholderID="itemsGoHere"
         GroupPlaceholderID="groupsGoHere">
         <LayoutTemplate>
             <table>
                 <asp:PlaceHolder runat="server" ID="groupsGoHere"></asp:PlaceHolder>
             </table>
         </LayoutTemplate>
         <GroupTemplate>
             <tr>
                 <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
             </tr>
         </GroupTemplate>
         <ItemTemplate >
                               
             <td><asp:CheckBox id="itemCheckBox" runat="server" /></td>
             <td>
                 <div class="checkboxlist-wrapper">
                     <asp:Label CssClass="checkboxlist-categories" ID="itemCheckBoxLabel" runat="server" Text='<%# Eval("CategoryName")%>'></asp:Label>
                 </div>
             </td>
         </ItemTemplate>
     </asp:ListView>  

    And here's the codebehind:

         public partial class WebForm1 : System.Web.UI.Page
         {
             private List<Category> _categories;
            
             protected void Page_Load(object sender, EventArgs e)
             {
                 LoadCategories();
             }
            
             void LoadCategories()
             {
                 List<Category> list = new List<Category>();
                 list.Add(new Category { CatId = new Guid(), CategoryName = "xxxx" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "wwwww" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "rrrrr rrrrr" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "yyyyy yyyy" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "4444444" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "hhhhhhhh hhhhhh hhhhhh" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "qq" });
                 list.Add(new Category { CatId = new Guid(), CategoryName = "xx66 666xx" });
                 _categories = list;
                 CategoryCheckList.DataSource = _categories;
                 CategoryCheckList.DataBind();
             }
            
             public List<Category> Categories { get => _categories; set => _categories = value; }
            
             public List<Category> GetData()
             {
                 return Categories;
             }
         }
            
         public class Category
         {
             public Guid CatId { get; set; }
             public string CategoryName { get; set; }
             public bool IsChecked { get; set; }
         }

    Tuesday, March 16, 2021 12:56 AM

All replies

  • User1535942433 posted

    Hi gpadvorac,

    I found there are a same case in Q&A and I have followed your reply.You could post your question in the old thread.

    https://docs.microsoft.com/en-us/answers/questions/313533/aspnet-get-id-of-checked-checkboxes-in-a-listview.html

    Best regards,

    Yijing Sun

    Tuesday, March 16, 2021 6:55 AM
  • User-1716253493 posted

    First, LoadCategories() in !IsPostBack block

    <asp:ListView ID="CategoryCheckList" runat="server" DataKeyNames="CatId"></asp:ListView>
            for(int i=0;i<CategoryCheckList.Items.Count;i++)
            {
                string id = CategoryCheckList.DataKeys[i].Values[0].ToString();   
            }

    Tuesday, March 16, 2021 3:13 PM
  • User-380220413 posted

    Hi jij sun,

    As you can see, in that old thread, I was told to post the question here instead.  So now that I'm here, do you have an answer for it?

    Thank you.

    Tuesday, March 16, 2021 8:27 PM
  • User-380220413 posted

    Thank you @oned_gk,

    Thank you and that worked for getting the Guid ID values.  however, I did not expect to have trouble getting the checked value from the checkboxes.  All Checked values are false even for the checkboxes that were checked.  I have revised the html and c#, please see the code below

    <asp:ListView ID="CategoryCheckList" DataKeyNames="CatId,IsChecked" runat="server" class="CListStyle"
        GroupItemCount="3" ItemPlaceholderID="itemsGoHere"
        GroupPlaceholderID="groupsGoHere">
        <LayoutTemplate>
            <table>
                <asp:PlaceHolder runat="server" ID="groupsGoHere"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <GroupTemplate>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemsGoHere"></asp:PlaceHolder>
            </tr>
        </GroupTemplate>
        <ItemTemplate>
    
            <td>
                <asp:CheckBox 
                    ID="itemCheckBox" 
                    runat="server"
                    Checked ='<%# Eval("IsChecked")%>'/></td>
            <td>
                <div class="checkboxlist-wrapper">
                    <asp:Label
                        CssClass="checkboxlist-categories"
                        ID="itemCheckBoxLabel"
                        runat="server"
                        Text='<%# Eval("CategoryName")%>'></asp:Label>
                </div>
            </td>
        </ItemTemplate>
    </asp:ListView>
    

    I Added IsChecked to the class:

    public class Category
    {
    public Guid CatId { get; set; }
    public string CategoryName { get; set; }
    public bool IsChecked { get; set; }
    }

    and this is from a button Click event:

    foreach (ListViewItem row in this.CategoryCheckList.Items)
    {
        //Check if current row is dataitem or not
        if (row.ItemType == ListViewItemType.DataItem)
        {
            //Get the checkbox control
            CheckBox checkBox = (CheckBox)row.FindControl("itemCheckBox");
            //Check if its selected or not
            if (checkBox.Checked)
            {
                //This is Always false;
                //...Add the code to update values to database...
            }
        }
    }

    Thank you.

    Tuesday, March 16, 2021 9:08 PM
  • User475983607 posted

    The problem is your design does not follow Web Forms standards.   Data bound controls typically work with one item at a time for CRUD operations.  Not several items as your example code seems to indicate.   

    Tutorial

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/

    ListView example from the tutorial above.  But you should go though the entire tutorial.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/display_data_items_and_details

    Look into the CheckBoxList if you want a list of checkboxes.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.checkboxlist?view=netframework-4.8

    Tuesday, March 16, 2021 9:39 PM
  • User-1716253493 posted

    Seem like postback issue, populate the grid inside if(!IsPostBack)

    Thursday, March 18, 2021 10:38 AM