Reputation: 139
I have an aspx page with repeater control on it used for showing the data.The user can modify the data inside the repeater but the data will not get saved until the SAVE button is hit. I also have an UNDO button on the page. If the user clicks this button, I want to reload the page and discard the changes made by user (similar to UNDO functionality in MS Office) I need to show him the same data that was shown when the page was loaded for the first time.
Kindly help me with this.
Thank you !
Upvotes: 9
Views: 51887
Reputation: 66641
This line of code is read the url as it is, and redirect you to the same page with out new post. Place it on button click, on code behind.
Response.Redirect(Request.RawUrl);
P.S. If you just make post, the input controls will keep their data, so you need to reload the page from the beginning, with the redirect. If you not make redirect you need to handle the controls and clear them.
Additional you can avoid the post back and round trip to the server and do the same on client side with javascript calling the 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 is adding a random number to the end of the url, or update it if exist to avoid to keep the page on cache. If you all ready avoid to keep the page on cache you can do the same with out the extra random parameter.
Upvotes: 12
Reputation: 3010
I suggest you to 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();
}
I hope this gives you the idea!
Upvotes: 0
Reputation: 2775
you can just add a submit button with no code in the event handler, this will trigger a page load and no actions will be performed apart from reloading the page.
Upvotes: 0