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?
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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
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...
}
}
}
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.
None
0 Points
8 Posts
Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 12:56 AM|gpadvorac|LINK
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:
And here's the codebehind:
Contributor
4040 Points
1568 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 06:55 AM|yij sun|LINK
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
All-Star
52813 Points
15768 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 03:13 PM|oned_gk|LINK
First, LoadCategories() in !IsPostBack block
Suwandi - Non Graduate Programmer
None
0 Points
8 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 08:27 PM|gpadvorac|LINK
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.
None
0 Points
8 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 09:08 PM|gpadvorac|LINK
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
I Added IsChecked to the class:
and this is from a button Click event:
Thank you.
All-Star
53711 Points
24031 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 16, 2021 09:39 PM|mgebhard|LINK
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
All-Star
52813 Points
15768 Posts
Re: Asp.net: Get ID of checked CheckBoxes in a ListView
Mar 18, 2021 10:38 AM|oned_gk|LINK
Seem like postback issue, populate the grid inside if(!IsPostBack)
Suwandi - Non Graduate Programmer