Monday, July 2, 2012

Clear Reset All Control in a form in asp.net


Using below function we can clear or reset all the controls available on the existing pages.


#region Clear All Controls
    public static void ResetFields(Control control)
    {
        //Reset All the textBoxes in panel.
        foreach (Control ctrl in control.Controls)
        {
            if (ctrl is TextBox)
            {
                string id = ctrl.ID;
                ((TextBox)ctrl).Text = String.Empty;
                ((TextBox)ctrl).Attributes["value"] = "";
            }
            else if (ctrl is DropDownList)
            {
                ((DropDownList)ctrl).ClearSelection();
                ((DropDownList)ctrl).Enabled = true;
            }
            else if (ctrl is RadioButtonList)
            {
                ((RadioButtonList)ctrl).SelectedIndex = 0;
            }
            else if (ctrl is RadioButton)
            {
                ((RadioButton)ctrl).Checked = false;
            }
            else if (ctrl is CheckBox)
            {
                ((CheckBox)ctrl).Checked = false;
            }
            else if (ctrl is CheckBoxList)
            {
                ((CheckBoxList)ctrl).ClearSelection();
            }
        }
    }
    #endregion

No comments:

Post a Comment