Getting Values of All CheckBox Controls in ASP.Net using C#
In ASP.Net, when we design a web form we use different kind of server controls such as textboxes, dropdownlists, radio buttons, checkbox controls etc. What if want to get the values associated to any specific type of control. In this tutorial we have tried to give the example of C# code to learn how to get the values associated to all the checkbox controls placed inside the ASP.Net Web Form i.e. server side form container. Before taking a look on example code you must have some idea about the C# foreach loop that enables to loop over the collection of items without using any counter variable. You can directly loop over the items and get the reference to a particular object reference in each iteration.
C# foreach Loop Over the Controls Collection
foreach (Control control in this.Page.Form.Controls)
{
if (control is CheckBox)
{
string checkboxValue = ((CheckBox)control).Text;
// use the above variable to save the value of each checkbox control.
//Response.Write(checkboxValue);
}
}
In the above C# code example we have used foreach loop specified with Control type object for assigning the reference to the control returned by the Controls collection in each iteration. The Page.Form.Controls collection will return the collection of all the child controls inside the server side form container on the web form. Inside the foreach loop you can see we have placed an if condition to test whether the control type in the current iteration is CheckBox or not. You can get the value of CheckBox control when the if condition returns true otherwise it will continue its iterations till the last control returned by the collection of controls placed inside the web Form.
Contine to next tutorial: How to get and display C# data types in ASP.Net to learn how retrieve the data types available in C#.

* will not be published
* hint: http://www.example.com