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?
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.
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.
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
jlrolin
Member
415 Points
133 Posts
How do I get Previous Page's URL?
Apr 10, 2007 08:15 PM|LINK
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?
AshokRaja
Contributor
2674 Points
478 Posts
Re: How do I get Previous Page's URL?
Apr 11, 2007 09:58 AM|LINK
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
www.ashokraja.me
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
Jay Khanpara
Member
85 Points
41 Posts
Re: How do I get Previous Page's URL?
Feb 26, 2008 11:37 AM|LINK
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?
Regards,
Jay Khanpara
gopalanmani
Star
7826 Points
1320 Posts
Re: How do I get Previous Page's URL?
Feb 26, 2008 05:25 PM|LINK
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
Gopalan Mani
My Tech blog
Jay Khanpara
Member
85 Points
41 Posts
Re: How do I get Previous Page's URL?
Feb 28, 2008 06:52 AM|LINK
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.
Regards,
Jay Khanpara
R1s
Member
2 Points
1 Post
Re: How do I get Previous Page's URL when i am using Response.Redirect()?
Oct 20, 2010 10:51 AM|LINK
How do I get Previous Page's URL when i am using Response.Redirect()?
varun.s
Member
2 Points
1 Post
Re: How do I get Previous Page's URL when i am using Response.Redirect()?
Feb 01, 2011 04:39 AM|LINK
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);