How to clear all form fields from code?

HTML has the type of input buttons to reset all the form fields to their original state in one step: <input type="reset" ... />.

Is there a similar easy way to reset all form fields of an aspx page form from code? Or is it necessary that reset all control one by one with help TextBox1.Text=string.Empty, TextBox2.Text=string.Emptyetc.?

Thanks in advance!

Update:

Context is a simple contact page / "Send us a message" with 8 asp: TextBoxes on the page (where the user enters a name, address, phone, email, message, etc.). Then he clicks the โ€œSendโ€ button, the encoded Onclick message processor sends an email to some administrator, and all the fields of the form filled in by the user must be emptied, and he receives a notification on the shortcut (โ€œMessage sent by blabla ...โ€)). I want the form fields to be cleared so that the user doesnโ€™t click again to submit, and the same message is sent a second time.

+5
source share
5 answers

fork , - , 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;
    ...
}

. , , , . :

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 );
    }
}

, , :

ClearControls( this );
+11

.

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);
        }
    }
}
+7

String.Empty , Response.Redirect(); . , , :

    Public void reset(Control control)
    {
      foreach (Control x in control.Controls)
      {
        if (x is TextBox)
        {
            (x as TextBox).Text = String.Empty;
        }
        if (x is DropDownList)
        {
            (x as DropDownList).SelectedIndex = 0;
        } 
        .
        .
        reset(x);
      }
    }

reset(this); , reset, . if , Control x.

+3

form.Controls.Clear() , , .

, , , : Reset "Button1", reset();

Reset:

   protected void resetButton_Click(object sender, EventArgs e)
   {
      TextBox1.Text=""; //set equal to empty string to all fields
   }

,

    protected void resetButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Test2.aspx", true);
    } 
+2

, , ViewState (EnableViewState=false) , .

, , , - .

+1

All Articles