• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

checkbox c# ASP.NET

I have several dynamic checkbox (i use html checkbox, but if necessary i use asp.net checkbox) with same name and same ID, how do I retrieve the values checked when an asp: button is clicked?

I use C# on ASP.NET page.

thanks for help.
0
fsouzabrasil
Asked:
fsouzabrasil
1 Solution
 
clinton_cunninghamCommented:
You cant have a page with multiple controls with the same ID. It should not compile and if it did it would be a really bad practice (same name also bad practice). These best thing you could do is to create a dynamic ID as you populate the page with controls. i.e. have a integer counter, i, and then increment it by 1 every-time you add a control.
so eventually you page will have a list of controls on the page like this:
<asp:checkboxlist id="CheckBoxList1" runat="server">
 <asp:Checkbox id="checkbox1" name="checkbox" runat="server">
 <asp:Checkbox id="checkbox2" name="checkbox" runat="server">
 <asp:Checkbox id="checkbox3" name="checkbox" runat="server">
<asp:checkbox/>

when your asp.net button is pressed you can cycle through all your controls like this:

string values;

foreach ( CheckBox box in CheckBoxList1)
 if if (box.Checked)
 {
   values = values + box.Value;    
}

0
 
fsouzabrasilAuthor Commented:
this checkboxes is grouped by category and i have one or more category, can i have dynamic CheckBoxList? how can I learn how to complete the loop? There is an equivalent function to the eval javascript, but to page compiled?
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now