Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Redirecting to the real referrer page during a postback

Total Hit ( 3178)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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

Then, when the "Save and Return" is clicked you retrieve the Url saved in the ViewState, and redirect to it:

Click here to copy the following block
Protected Sub SaveAndReturn_Click(ByVal sender As Object, ByVal e As EventArgs)
  ' do something here, e.g. add/update some DB records
  
  ' redirect to the previous page
  Response.Redirect(ViewState("ReferrerUrl").ToString())
End Sub



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.