Tuesday, July 6, 2010

UrlReferrer property in ASP.Net

For a scenario where you want to reload the previous page on click event of the Back Button of New redirected page, the following solution will be helpful.

Sol:- You need to use UrlReferrer property of Request object to access the previous page URL.

Code:-
In the code behind file of the New Page.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //check if the webpage is loaded for the first time.
{
ViewState["PreviousPage"] =
Request.UrlReferrer;//Saves the Previous page url in ViewState
}
}

protected void btnBack_Click(object sender, EventArgs e)
{
if (ViewState["PreviousPage"] != null) //Check if the ViewState
//contains Previous page URL
{
Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to
//Previous page by retrieving the PreviousPage Url from ViewState.
}
}

No comments: