go back to previous page when Cancel button hit

  • Thread starter Thread starter wiewiek
  • Start date Start date
W

wiewiek

Is there a way to go back to the previous page when a button (Cancel
button) is hot? It's an ASP control.

Thanks
 
Is there a way to go back to the previous page when a button (Cancel
button) is hot? It's an ASP control.

You mean "hit?" I will presume that the title is correct and that your
message has a typo.

It will be a little tricky, as we're dealing with an ASP.Net WebForm here,
and very likely, the "previous page" will be the same page (if it has posted
back) in your browser's history. What you would need to do is to keep track
of the "previous page" by storing it's URL (Request.UrlReferer) in ViewState
when the page is initially loaded, and only then. Thus, the URL of the
"previous page" will be persisted in ViewState across postbacks.

From there, it's a simple matter of handling the OnClick event of the
Control with a Response.Redirect to that URL.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
If you're just looking to go back, you can just add an attribute with
JavaScript to save you the trouble of handling the postback:

\\\
private void Page_Load(object sender, System.EventArgs e)
{
// Add attribute to go back one page.
btnCancel.Attributes.Add("onClick", "history.back(); return false;");
}
///

Note that you must include the "return false;" snippet as well to keep the
page from submitting. This is needed because ASP.NET buttons translate into
submit buttons in the rendered HTML code at the browser.

Hope this helps!

Eric
 
Back
Top