Introduction:-
Some time we come across with the scenario where we need the function to select\deselect the checkboxes
from the server side code.The following code is going to describe the scenarion where we have one Panal Control which is having the multiple checkboxe
Refer the following link to know how to add dyanamic checkboxes to Panal in ASP.NET using C#.
How to generate Dynamic Checkbox in ASP.Net using C#
Code sample :-
//Drag and drop a panal control in asp.net web page
//Following will be the auto generated code with user defind controlName
protected System.Web.UI.WebControls.Panel PnlTimeExpence;
//Decleare one checkbox object
CheckBox chkList1;
//write a code to add dyanamic checkboxes
..
..
.. //code to add dyanamic controls(checkboxes). Check link above for adding dynamic checkboxes.
..
..
//Now we have a Panel control ready with multiple checkboxes.
//Fuction to select\Deselect checkboxes
private void SelectAllCheckBoxControls(Control parent,bool checkedVal)
{
try
{
string strLeave=string.Empty;
foreach (Control child in parent.Controls)
{
if (child.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
{
CheckBox chk = (CheckBox)child;
chk.Checked=checkedVal;
}
}
}
catch(Exception exp)
{
throw new Exception("SelectAllCheckBoxControls " + exp.Message);
}
}
//Generate event on which we want to perform the select\deselect operation
//Following code is using a checkbox control to select \deselect the Event
private void chkReport_CheckedChanged(object sender, System.EventArgs e)
{
try
{
//Calling a userdefind function
SelectAllCheckBoxControls(PnlTimeExpence,chkReport.Checked);
}
catch(Exception exp)
{
throw new Exception("chkReport_CheckedChanged " + exp.Message);
}
}