ASP.NET Trick – How to Return to the previous page regardless of where you came from?

ASP.NET has a very sophisticated methods to navigate from one page to another page but there isn’t any method which allow programmers to wire the page to return to the previous page you came from. Think of the situation of page used in the workflow and later same page can be accessed from the other areas of the system and same button should navigate to the user to the previous page regardless of what subsystem user visited from.

I have used (as many of you might have) following pattern in past and it worked great. Though of sharing this for wider community.

Create a Textbox or Session variable. Here I’m using a textbox and make the visible property of the textbox to false.

Write the below code in the Page_Load event()
if(!IsPostBack)
{
this.txtRequestUri.Text= this.Page.Request.UrlReferrer.AbsolutePath.ToString();
}

In the BACK or CANCEL or FINISH button click event handler, use the following code

string requestPage = this.txtRequestUri.Text.ToString();
Response.Redirect(requestPage, true);

Nik

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s