locked
How do I get Previous Page's URL? RRS feed

  • Question

  • User-1471465568 posted

    I want to make a Return column visible on my GridView based on a condition, but I want the NavigateUrl to be the previouspage URL plus the ID that I am sending back to that page.

    Currently, I use this:
    Dim previousPage As String = Page.Request.UrlReferrer.ToString

    This gives me the previous referral page's URL, BUT... if I hit Find to search for something and then the search lists to my GridView, previousPage errors with a Object Not Set to Instance, etc. Error. Why is this? And does anyone have a way of grabbing the previous page URL?

    Tuesday, April 10, 2007 4:15 PM

Answers

  • User960609757 posted

    string referer = Request.UrlReferrer.ToString();

    One thing to remember is that UrlReferrer will change to your pagename when you post back to the server.. so you should maybe store the value in ViewState on the first page load of the page if you are wanting to redirect the user to the previous page after a couple postbacks to the same page.

    if( !IsPostBack )
    {
     ViewState["PreviousPageUrl"] = Request.UrlReferrer.ToString();
    }

    The situations where it does work include the following methods of a browser loading a URL:

    clicking on a straight HTML <a href> link;
    submitting a form, using POST or GET, from a submit button, <input type=image> or client-side script (form.submit())

    The situations where it doesn't work:

    using Response.Redirect / Server.Transfer;
    clicking on a Favorite, History, or the recently-typed URLs list;
    clicking on 'Home' in IE's toolbar, or an item in IE's 'Links' toolbar;
    using location.href or location.replace() in client-side JScript/JavaScript/VBScript;
    using HierMenus (details);
    typing the URL directly in the browser and hitting Enter or clicking 'Go';
    launching a clickable URL from an e-mail or MS Office document;
    using Response.AddHeader  or <meta http-equiv=refresh> to redirect;
    loading the URL with XML

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 26, 2008 1:25 PM

All replies

  • User2133533947 posted

    If your page is stright away opened without any redirection from any other page  Request.UrlReferrer will be null. So check whether it is null or not before using that code.

    if(Request.UrlReferrer!=null)

    Response.Write(Request.UrlReferrer.ToString());

    Let me know if you need any further help

    Wednesday, April 11, 2007 5:58 AM
  • User-1333561775 posted

    Hi,

    I tried with your code but its giving the same page url only, what should I do?

    I am running my application on localhost, so will be a reason to give the same url everytime (means only current page's url only) ????????

    After navigating three pages, my "sendmail" page is opening (Where I am fetching the previous page's url)

    Any ideas?

     

    Tuesday, February 26, 2008 7:37 AM
  • User960609757 posted

    string referer = Request.UrlReferrer.ToString();

    One thing to remember is that UrlReferrer will change to your pagename when you post back to the server.. so you should maybe store the value in ViewState on the first page load of the page if you are wanting to redirect the user to the previous page after a couple postbacks to the same page.

    if( !IsPostBack )
    {
     ViewState["PreviousPageUrl"] = Request.UrlReferrer.ToString();
    }

    The situations where it does work include the following methods of a browser loading a URL:

    clicking on a straight HTML <a href> link;
    submitting a form, using POST or GET, from a submit button, <input type=image> or client-side script (form.submit())

    The situations where it doesn't work:

    using Response.Redirect / Server.Transfer;
    clicking on a Favorite, History, or the recently-typed URLs list;
    clicking on 'Home' in IE's toolbar, or an item in IE's 'Links' toolbar;
    using location.href or location.replace() in client-side JScript/JavaScript/VBScript;
    using HierMenus (details);
    typing the URL directly in the browser and hitting Enter or clicking 'Go';
    launching a clickable URL from an e-mail or MS Office document;
    using Response.AddHeader  or <meta http-equiv=refresh> to redirect;
    loading the URL with XML

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 26, 2008 1:25 PM
  • User-1333561775 posted

    Hi gopalanmani,

    Great job !!!!!!! I got the answer but thank for giving such a good information about when it doesnt work.

     Thank you very much.

    Thursday, February 28, 2008 2:52 AM
  • User-177920467 posted

    How do I get Previous Page's URL when i am using Response.Redirect()? 

    Wednesday, October 20, 2010 6:51 AM
  • User1549316151 posted

    if(!IsPostback)

          

    if(!string.IsNullOrEmpty(Request.UrlReferrer.ToString())

    {

    sPreviousPageUrl = Request.UrlReferrer.ToString();

    }

     

    }

     

     

    public string sPreviousPageUrl

    {

     

    get { return (string)ViewState["sPreviousPageUrl"]; }

     

    set { ViewState["sPreviousPageUrl"] = value; }

    }

     

    Response.Redirect(sPreviousPageUrl);

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Tuesday, February 1, 2011 12:39 AM