Redirecting to the real referrer page during a postback
Say that you have a page loaded from another page, and that in this page you have a "Save and Return" button that does something and returns to the previous page. For example you may have an Orders page that loads a Details page, where you make some changes and press the button to save the changes and return to the Orders page. You may think that the following code would work just fine:
Response.Redirect(Request.UrlReferrer.ToString())
But this isn't the case. In fact, when a web form submits itself and generates a postback, the referrer page becomes the page itself! The solution is very simple though. When the page is first loaded (i.e. it is not a postback), you save the real referrer page in the page's ViewState:
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
' Save the referrer Url
ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()
End If
End Sub
It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com. Already a member?
To become a member of DevX.com create your Member Profile by completing the form below. Membership is free!
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.