go back to previous page when Cancel button hit

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
 
K

Kevin Spencer

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top