Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all form fields from code-behind?

HTML has an input button type to reset all fields in a form to their initial state in one step: <input type="reset" ... />.

Is there a similar simple way to reset all form fields of an aspx page from code-behind? Or is it necessary to reset all controls one by one with TextBox1.Text=string.Empty, TextBox2.Text=string.Empty, etc. ?

Thanks in advance!

Update:

Context is a simple Contact/"Send us a message" page with 8 asp:TextBoxes on the page (where the user enters the name, address, phone, email, message, etc.). Then he clicks on submit, the Onclick message handler in code-behind sends an email to some administrator, and all the form fields the user filled in should be emptied and he gets a notification in a label ("Message sent blabla..."). I want to have the form fields cleared to avoid that the user clicks again on submit and the same message is sent a second time.

like image 353
Slauma Avatar asked Apr 10 '10 14:04

Slauma


2 Answers

You need only write a fork for each type of control unless one of the control has something special that needs to be done to reset it.

foreach( var control in this.Controls )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;
    ...
}

ADDITION You asked how to clear controls even ones that are buried. To do that, you should create a recursive routine like so:

private void ClearControl( Control control )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;
    ...

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}

So, you would call this by passing the page:

ClearControls( this );
like image 186
Thomas Avatar answered Oct 05 '22 05:10

Thomas


Refer this link for more information

http://www.freshcodehub.com/Article/3/clear-all-fields-like-textbox-dropdownlist-checkbox-radiobutton-label-after-form-submission-in-aspnet-c

public void ClearControls(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        if ((c.GetType() == typeof(TextBox)))  //Clear TextBox
        {
            ((TextBox)(c)).Text = "";
        }
        if ((c.GetType() == typeof(DropDownList)))  //Clear DropDownList
        {
            ((DropDownList)(c)).ClearSelection();
        }
        if ((c.GetType() == typeof(CheckBox)))  //Clear CheckBox
        {
            ((CheckBox)(c)).Checked = false;
        }
        if ((c.GetType() == typeof(CheckBoxList)))  //Clear CheckBoxList
        {
            ((CheckBoxList)(c)).ClearSelection();
        }
        if ((c.GetType() == typeof(RadioButton)))  //Clear RadioButton
        {
            ((RadioButton)(c)).Checked = false;
        }
        if ((c.GetType() == typeof(RadioButtonList)))  //Clear RadioButtonList
        {
            ((RadioButtonList)(c)).ClearSelection();
        }
        if ((c.GetType() == typeof(HiddenField)))  //Clear HiddenField
        {
            ((HiddenField)(c)).Value = "";
        }
        if ((c.GetType() == typeof(Label)))  //Clear Label
        {
            ((Label)(c)).Text = "";
        }
        if (c.HasControls())
        {
            ClearControls(c);
        }
    }
}
like image 28
Kamal Pratap Avatar answered Oct 05 '22 05:10

Kamal Pratap