0
Posted on 4:06 AM by prajeesh and filed under ,
In some applications we may need to reset all controls in using a single "Reset" button click, here is the c# code to achieve this.

public static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)
{
foreach (Control cntrl in pagecontrols)
{
foreach (Control mycontrols in cntrl.Controls)
{
if (txtbox)
{
if (mycontrols is TextBox)
{
(mycontrols as TextBox).Text = string.Empty;
}
}
if (dropdownlist)
{
if (mycontrols is DropDownList)
{
(mycontrols as DropDownList).SelectedIndex = 0;
}
}
if (label)
{
if (mycontrols is Label)
{
(mycontrols as Label).Text = string.Empty;
}
}
}
}
}

We can call this function using following format if you want to clear all controls except label

FormControl.ResetControls(this.Controls, true, true, false);

Shout it kick it on DotNetKicks.com
0
Responses to ... Resetting all controls in an asp.net form