Using Request.UrlReferrer to make a serverside back button

First Create a ViewState property to hold your PreviousUrl.

public string PreviousUrl
 {
 get
 {
 if (ViewState["RV_ReturnUrl"] != null)
 {
 return ViewState["RV_ReturnUrl"].ToString();
 }
 else
 {
// Your else condition - i return the user to home page if PreviousUrl is null
return Globals.NavigateURL(PortalSettings.HomeTabId);
 }
 }
 set
 {
 ViewState["RV_ReturnUrl"] = value;
 }
 }

Next, you will need to save the Referrer URL on first page load. This is becuase the Referrer URL will change on every postback.

protected override void OnLoad(EventArgs e)
 {
 base.OnLoad(e);
 try
 {
 if (!Page.IsPostBack)
 {
    // Check for null referrer (user just typed in the url)
    if(Request.UrlReferrer != null)
       PreviousUrl = Request.UrlReferrer.ToString();
 }
 catch (Exception ex)
 {
 Exceptions.ProcessModuleLoadException(this, ex);
 }
 }

Lastly, use the PreviousUrl as necessary to Redirect

Response.Redirect(PreviousUrl, false);

Note – this is a DNN implementation. Use this as a guide for a vanilla ASP.NET implementation.

Luke

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s