tags:

views:

90

answers:

3

Hello,

Is there an easy way to reset all the fields in a form. I have around 100 controls in my asp.net form and there is submit and reset buttons.

How do I make all values in the fields null when user hits reset button?

I have a lot of dropdown boxes, textboxes, checkboxes

Please help

+2  A: 

Try adding an:

<input type="reset" value="Clear" />

to your form.

Darin Dimitrov
So far I know that would initialize forms field to the initial state. If the initial state for a texbox was "HELLO" It will reset to that value. I don't think it's what the user needs.
Claudio Redi
+1  A: 

Add this to the server-side handler of the cancel button:

Response.Redirect("~/mypage.aspx", true);
IrishChieftain
This would lose all viewstate data, which might not be desirable.
diamandiev
If we're clearing out all the fields, we obviously don't need to retain the ViewState in the first place... we're simply re-loading the page afresh which is the simplest way of doing this. He specified that all fields were being cleared :-)
IrishChieftain
A: 

Loop through all of the controls on the page, and if the control is type TextBox, set the Text property to String.Empty

protected void ClearTextBoxes(Control ctrl)
{
    foreach (Control ctrl in ctrl.Controls)
    {
        if(ctrl is TextBox)
        {
             TextBox t = ctrl as TextBox;

             if(t != null)
             {
                  t.Text = String.Empty;
             }
        }
        else
       {
           if (ctrl.Controls.Count > 0)
           {
               ClearTextBoxes(ctrl);
           }
        }
    }
}

Then to call it in your click event like this:

 ClearTextBoxes(Page);
TheGeekYouNeed