Thursday, August 16, 2012

How to get previous page name in c#?

1) Session Variable Previous page/Viewstate Variable
2) QueryString http://yoursite.com/thispage?from=PreviousPage
3) Using Property of a class e.g. By setting Property set while redirecting and get in target page.
4) By following methods
public string GetCurrentPageName()
{
 string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
 System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
 string sRet = oInfo.Name;
 return sRet;
}

public string GetPreviousPageName()
{
 string sPath = Page.PreviousPage.Request.Url.AbsolutePath;
 System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
 string sRet = oInfo.Name;
 return sRet;
}

protected void Page_Load(object sender, EventArgs e)
{
 if (Request.UrlReferrer != null)
 {
   //geting the name of the previous page in a variable sPrevPage  
   string sPrevPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
   Response.Write("Previous page is - " + sPrevPage);
 }
}

No comments:

Post a Comment