开发者

How to clear all form fields from code-behind?

HTML has an input button type to re开发者_开发技巧set 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.


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


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


using the manual approach of String.Empty for each and every Textbox or any other field will be cumbersome, also by using Response.Redirect(); it will be difficult to show any confirmation message or same. So, on reading so many blogs i have found a reliable approach so far:

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


          

use this code as reset(this); in your page wherever you want to reset or clear the values. At end of the if conditions do not forget to use the function recursively using the same

`Control` object x.


Using form.Controls.Clear() is not such a good approach because it will clear the entire form and you will even lose all the buttons on the form.

Instead if you just want to clear all the form fields like text fields and radio buttons I would recommend you try the following: If you have a Reset button “Button1” then on click call a function reset();

In the reset function:

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

Or redirect to same page by terminating the previous page

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


For your scenario the easiest way to clear the fields, in my opinion, is to turn off the ViewState (EnableViewState=false) of the controls you want to appear blank after the submit.

Or perhaps for the whole page unless there is some state you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜