How can I reset create an asp.net form after postback?

I try after the btnCreate_OnClick event to assign a reset value to the default form, like the first page_load. The problem occurs after PostBack, each text field and other controls reloads the ViewState value. I cannot deactivate viewstate due to server event when selecting DropDownList. The only way I have found so far is to redirect to myself after the click event, but it loads the page twice and is the reason for the bad decision. I tried ViewState.Clear () and updated UpdatePanel but was unsuccessful.

I could do a loop for all the controls and set txtXXXXX.Text == "", but I'm sure this is not a good idea.

Something like Page.Reset () would be just perfect, but that doesn't exist.

Any thoughts on my issue?

thanks

+4
source share
6 answers

If you are healthy, 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, so there is no reason to fix it.

+12
source

We can reset an ASP.NET form page with only two lines of code

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

Auto-redirecting becomes complicated due to viewstate.

There is an “reset” html input type for buttons, but I'm not sure if or any kind of integration msft placed in viewstate / asp.net for this. It usually works for simple javascript forms.

Example:

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

from google ---- ^

+2
source

One way, not necessarily perfect, is to reset the defaults using Javascript. If this is a large form, it can be ugly, but will prevent the need for self-redirecting.

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

0
source

I don’t know if this helps, but I change the name of each input in the form that I want to get fresh values ​​using javascript before submitting the form, since the .net page can no longer match the values ​​from the form with the controls for the page, it reloads them like if there were no postback. I also add a new value to the form, so I know which button submitted the form and uses this logic to decide what to load into all the controls and how to process the form data, of course.

 $("#Bset").children().click(function() { //all the btns click function $.each($("form").find("input"), function(e,v) { //could filter this to subset of inputs $(v).attr("name", "_" + $(v).attr("name")); // ctrl1 becomes _cntrl1 }); $("form").append("<input type='hidden' id='b' name='b' value='" + $(this).text() + "' />").submit(); }); 

then in code <

 protected void Page_Init(object sender, EventArgs e) { id = int.Parse(Request.QueryString["id"]); bk = db.dc.getDetailBK(id).Single(); if (Request.Form.Count > 0) doPostBack(); mrl = (from a in db.dc.getMetricQTD(id, null, null, loadSavedGoals) select a).ToList(); } 

Then I can do something in dopostback, processing form data, interacting with db, etc., which can change mrl values, load and update data on inputs mrl-bound, regardless of whether they were changed or not the form.

another alternative would be to bind some buttons to using a web service to handle your interaction with db, and then call window.location to refresh the page.

$. Ajax ({
url: "webservice / dbfunction?"
data: {which btn pressed, some form values, etc.}}
success: function () {window.location ("samepage.aspx? id = xxx");}
...
});

this would also avoid server-side response redirection.

0
source

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

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

This avoids double page_load and loop. We do not need to update the UpdatePanel as it runs before rendering.

In a more complex web application, we may have to redirect, as most people seem to accept this as a solution.

The default setting for each control was better for our case.

thanks

-2
source

All Articles