How do I redirect a user back to the page they came from?
On my aspx page, I have a cancel button. If the user clicks it, I want to send the user back to the page that they navigated from. Can I do this with the Response.Redirect?
Printable View
How do I redirect a user back to the page they came from?
On my aspx page, I have a cancel button. If the user clicks it, I want to send the user back to the page that they navigated from. Can I do this with the Response.Redirect?
Yes,
You can just use: response.redirect("your_page.aspx")
Kenny
I know that, but I am not sure which page the user came from. The user could have came from any number of the pages.
Try to set the original page as a session variable before you redirect. That way you can use it in the second page. Try something like this -
On the first page:
Session("fromWhere")="firstPage.aspx"
Response.Redirect("secondPage.aspx")
On secondPage.aspx:
Hyperlink1.NavigateUrl = Session("fromWhere")
Simple enough, eh? Let us know if it works ok.
Ooogs
I was trying not to use session variables if I can help it. I want to keep the performance as fast as possible without tasking the server too much. I heard using session variables can eat away at the server if you have a lot of sessions and their variables being managed at one time.
I will go that route if I have to though, thanks for the input. If anyone else has some other methods, they would be appreciated also.
You can store the user in hidden fields and then just use the same idea as above.
Response.Redirect(Request.UrlReferrer.PathAndQuery)
- Justin_
Awesome! Thanks a lot, that is what I was looking for.
Does the:
Response.Redirect(Request.UrlReferrer.PathAndQuery)
need to be wrapped in a "If is NotPostBack Then" loop? Just curious as I was unsucessfull using it.
Thanks...Ooogs
I haven't had the chance to try it yet, hope it works...
If you figure it out, post it here.
I found this on the MSDN Library site:
http://msdn.microsoft.com/library/de...errertopic.asp
Put in the Page_Load method:
Dim refHost As String = Request.UrlReferrer.Host
If NOT Page.IsPostBack AND (refHost = Request.Url.Host) Then
'The user came from a place on your website
Response.Redirect(Request.UrlReferrer.PathAndQuery)
Else
If NOT refHost = Request.Url.Host Then
'The user did not come from your website
Else
'The page was posted back
End If
End If
- Justin_