Clear value of controls on a page

This Code is used to clear the value of TextBox, DropDownList, CheckBox, CheckBoxList controls available in any container control.

in this Sample Code we need to pass the instance of the form (Page obj) and the Container Control's ID.

Sample Code:
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[i].Selected =false;
}
}
}
}

0 comments: