tags:

views:

2692

answers:

5

I am trying after the btnCreate_OnClick event to reset the form to it's default value just like the first page_load. The problem is after PostBack, every textbox and other controls, reloads the ViewState value. I cannot deactivate viewstate because of server event on DropDownList selection. The only way I found so far is to Redirect to self after the click event, but this loads the page twice and is therefor a bad solution. I have try ViewState.Clear() and update the UpdatePanel, but was unsuccessful.

I could do a loop for all controls and set the txtXXXXX.Text == "", but I'm quite sure it's not the best idea.

Something like Page.Reset() would have been just perfect but it doesn't exist.

Any thought on this problem of mine?

Thanks

A: 

One way, not necessarily ideal, is to reset the values to their defaults using Javascript. If it is a large form, it can be ugly, but will prevent the need to do a self-redirection.

You also might try Server.Transfer instead of Response.Redirect(/self/)

Chris Ballance
+5  A: 

If workable, I usually just use Response.Redirect to reload the same page from scratch.

An initial GET request to a page usually costs less than subsequent POSTs anyway, so there's not much reason to avoid it.

Dave Ward
This will only work in cases where the record in question loads up via a redirect to the page; hence this cannot be used always!
renegadeMind
+1  A: 

Self redirecting gets tricky because of viewstate.

There is an html input type "reset" for buttons, but I'm not sure what or any integration msft has put into viewstate/asp.net for this. It generally works for simple javascript forms.

ex:

<input type="button" value="Reset" onclick="document.<formId>.reset();">

from google ----^

Will Charczuk
A: 

In our case the best performance solution was to set manually for each control the default value in the click event ex:

textbox1.Text = null;
textbox2.Text = null;

This avoid the double page_load and the loop. We don't event have to update the UpdatePanel since it executes before render.

Maybe in a more complex web application we would have to Redirect as most people seem to accept this as a solution.

Setting per control the default value was better suited to our case.

Thank you

lucian.jp
+1  A: 

We can reset the ASP.Net Form Page with just 2 lines of code

protected void Button_Reset_Click(object sender, EventArgs e) { Session["ViewState"] = null; Response.Redirect("/Roster/DRAC/Create.aspx"); }