How to reload the same page with the click of a button, with data from the database and ignoring changes

I have an aspx page with a repeater control used to display data. The user can change the data inside the repeater, but the data will not be saved until the SAVE button is pressed. I also have a UNDO button on the page. If the user clicks this button, I want to reload the page and discard the changes made by the user (similar to the UNDO functions in MS Office). I need to show him the same data that was shown when the page was loaded for the first time.

Please help me with this.

Thanks!

+8
source share
3 answers

This line of code reads the URL as is, and redirects you to the same page without a new message. Put it at the click of a button, on the code behind.

Response.Redirect(Request.RawUrl); 

PS If you just post, the input controls will save their data, so you need to reload the page from the very beginning, with redirection. If you are not redirecting, you need to process the controls and clear them.

In addition, you can avoid the inverse and OnClientClick="return ReloadPage();" to the server and do the same on the client side using javascript calling OnClientClick="return ReloadPage();" with JavaScript code:

 function ReloadPage() { top.location.replace( updateURLParameter(document.location.href, "_rnd", Math.random()) ); return false; } // // this function is an improved version of // http://stackoverflow.com/a/10997390/159270 // function updateURLParameter(url, param, paramVal) { var TheAncor = null; var newAdditionalURL = ""; var tempArray = url.split("?"); var baseURL = tempArray[0]; var additionalURL = tempArray[1]; var temp = ""; if (additionalURL) { var tmpAncor = additionalURL.split("#"); var TheParams = tmpAncor[0]; TheAncor = tmpAncor[1]; if(TheAncor) additionalURL = TheParams; tempArray = additionalURL.split("&"); for (i=0; i<tempArray.length; i++) { if(tempArray[i].split('=')[0] != param) { newAdditionalURL += temp + tempArray[i]; temp = "&"; } } } else { var tmpAncor = baseURL.split("#"); var TheParams = tmpAncor[0]; TheAncor = tmpAncor[1]; if(TheParams) baseURL = TheParams; } if(TheAncor) paramVal += "#" + TheAncor; var rows_txt = temp + "" + param + "=" + paramVal; return baseURL + "?" + newAdditionalURL + rows_txt; } 

The above code adds a random number to the end of the URL or updates it if it exists to avoid caching the page. If you are all ready to avoid storing the page in the cache, you can do the same without the extra random parameter.

+12
source

you can simply add a submit button without code in the event handler, this will lead to page loading, and no actions will be performed except reloading the page.

0
source

I suggest you save the DataTable using ViewState

 protected void Page_Load (object sender, EventArgs e) { if(!isPostBack) { DataTable Dt = new DataTable(); ProcessIt();//I assume you know better how to process your data) ViewState("Tbl") = Dt; } } protected void BtnSave_Click (object sender, EventArgs e) { SaveIt();//Save your changes first ViewState("Tbl") = Dt;//store back the datatable } protected void BtnCancel_Click (object sender, EventArgs e) { DataTable Dt = ViewState["tbl"]; GridView.DataSource = Dt; GridView.DataBind(); } 

Hope this gives you an idea!

0
source

Source: https://habr.com/ru/post/1393951/


All Articles