0
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.
We can call this function using following format if you want to clear all controls except label
- 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;
- }
- }
- }
- }
- }
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);
FormControl.ResetControls(this.Controls, true, true, false);
Post a Comment