ASP.NET - Working with Controls -Resetting Form Controls

Code Snippet for Resetting the controls in Form by calling single method

public void ResetFormControls(ControlCollection Pagectrls, bool TextBox, bool Label, bool DropdownList, bool RadioButtonList,bool CheckBoxList)
        {
//looping through  Form controls
            foreach (Control cntrl in Pagectrls)
            {
                foreach (Control InputControl in cntrl.Controls)
                {
                    if (TextBox)
                    {
//if control is text box clear
                        if (InputControl is TextBox)
                        {
                            (InputControl as TextBox).Text = string.Empty;
                        }
                    }
                    else if (Label)
                    {
//if control is Label clear
                        if (InputControl is Label)
                        {
                            (InputControl as Label).Text = string.Empty;
                        }
                    }
                    else  if (DropdownList)
                    {
//if control is DropDownList reset to default
                        if (InputControl is DropDownList)
                        {
                            (InputControl as DropDownList).SelectedIndex = 0;
                        }
                    }
                    else if (RadioButtonList)
                    {
//if control is RadioButtonList reset to default
                        if (InputControl is RadioButtonList)
                        {
                            (InputControl as RadioButtonList).SelectedIndex = 0;
                        }
                    }
                    else if (CheckBoxList)
//if control is CheckboxList select defualt index
                        if (InputControl is CheckBoxList)
                        {
                            (InputControl as CheckBoxList).SelectedIndex = 0;
                        }                   
                }
            }
        }

Calling the function in Reset Button to reset control values

 //calling in resetbutton click
        protected void btnReset_Click(object sender, EventArgs e)
        {
            ResetControls(this.Controls, true, true, true,true,true);
        }
Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.