views:

250

answers:

2

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.

+6  A: 

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 );
Thomas
You should probably include some recursion if you have databindings, controls inside templates and custom controls.
Mikael Svenson
Nice simple thing! (will be especially simple in my case since I only have TextBoxes on the page). Thank you!
Slauma
There is a problem: My TextBoxes are inside of the `Content` element for a `ContentPlaceHolder` because my page is derived from a MasterPage. The `this.Controls` collection contains only one element which is the MasterPage itself (or something derived from it) with the `ID=ctl00`. I was searching around in debugger but can't find any collection of objects which represent my TextBoxes.
Slauma
@Slauma - I've amended my response so that you can recursively clear each control.
Thomas
@Thomas: Great, it works! Thanks! (I only had to replace the anonymous `var` in the foreach loop by `Control` since the compiler complained that `childControl` is an `object` and not a `Control`).
Slauma
@Slauma - I corrected my post accordingly. Glad I could help.
Thomas
A: 

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.

Mikael Svenson
Hm, I've just tested it (for one of TextBoxes and also the option to turn off the ViewState of the whole page). But it doesn't work: All TextBoxes are still filled after the postback.
Slauma
If you do a regular postback (not ajax) with EnabledViewState=false on the page or a control, there should be no way those fields should have values, unless you set them in code somewhere.
Mikael Svenson
I've created a test web site (one TextBox+one Button, EnableViewState="false" on page level and also for the TextBox): Same behaviour, Values in the TextBox are preserved after PostBack. I've googled a bit and found explanations: 1) http://www.dotnetfunda.com/articles/article760-common-misconception-regarding-viewstate-and-textbox-.aspx and 2) http://geekswithblogs.net/AaronLi/archive/2007/05/20/112615.aspx
Slauma
@Slauma Learn something new every day :)
Mikael Svenson