Wednesday, November 28, 2012

Process of getting the referring page on Page Load event in ASP .NET

When a page loads, in order to get the name of the page that sent you there, all you need to use is:
   1:  Request.UrlReferrer.ToString();

You can create a global variable to hold it: 

   1:  String sReferrer = "";

Then, in the Page_Load event, assign it:

   1:  if(!Page.IsPostback)
   2:  {
   3:    sReferrer = Request.UrlReferrer.ToString();
   4:  }

Or, you can put it in ViewState at that time: 
   1:  if(!Page.IsPostback)
   2:  {
   3:    ViewState("Referrer") = Request.UrlReferrer.ToString();
   4:  }

From there, on out, within that page, you can use either the variable, or View State, within that page as a link, or whatever you need it for.