question

moondaddy-8531 avatar image
0 Votes"
moondaddy-8531 asked YijingSun-MSFT commented

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

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 how to loop through the list to get these IDs in the postback. 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; }
 }


Thank you.



dotnet-csharpdotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered YijingSun-MSFT commented
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DuaneArnold-0443,

Querying the collection is not the question. the question is how to assign an ID to each checkbox when the page is generated so in the postback, when I find a checkbox that's checked, I will have an ID to associate it with.

0 Votes 0 ·

Hi @moondaddy-8531 ,
As far as I think,there is no way to assign an ID to the checkbox unless you could dynamically create checkboxes. If you dynamically create checkboxes,you could set each checkboxes' ID.You could refer to this article:
https://stackoverflow.com/questions/38048493/dynamically-created-checkbox-in-c-sharp-using-asp-net-show-vertically

0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

They have a Webform forum in ASP.NET forums. Someone can help you there.

https://forums.asp.net/18.aspx/1?Web+Forms

You should learn how to use ASP.NET Blazor, Razor pages or MVC, becuase ASP.NET Webform has become legacy technology.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.