65.9K
CodeProject is changing. Read more.
Home

Simple and easy way to clear all the input fields in the form.

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Feb 27, 2010

CPOL
viewsIcon

12534

This is an easy way to clear all input fields in the form.

ALTERNATIVE #1: via Server side Codebehind

Something like...

private void ResetFormControlValues(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.Controls.Count > 0)
            {
                ResetFormControlValues(c);
            }
            else
            {
                switch(c.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.TextBox":
                        ((TextBox)c).Text = "";
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        ((CheckBox)c).Checked = false;
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        ((RadioButton)c).Checked = false;
                        break;
                  
                }               
            }
        }
    }