Clear value of controls on a page

Lakhangarg
Posted by Lakhangarg under ASP.NET category on | Views : 2159
If you have place your Controls in a container control then you can use the following method to clear the values of all the controls with the help of a single method.
In this code you need to pass page's Object and if of container control.

public static void ClearControls( Page obj, string strContentHolder)
{
foreach (Control c in obj.Master.FindControl (strContentHolder).Controls)
{

if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{

TextBox t = (TextBox)c;
t.Text ="";
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList"))
{
DropDownList ddl = (DropDownList)c;
ddl.SelectedIndex = 0;
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
{
CheckBox chk = (CheckBox)c;
chk.Checked =false;
}

if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBoxList"))
{
CheckBoxList chkList = (CheckBoxList)c;
for (int i = 0; i < chkList.Items.Count; i++)
{
chkList.Items[i].Selected =false;
}
}
}
}


Thanks & Regards
Lakhan Pal Garg

Comments or Responses

Login to post response