Saturday, July 3, 2010

Navigate to previous page in ASP.NET

Requirement: To Get the URL of the previous page from which I was redirected to the New page in the Button click event handler of the New Page so that I can redirect the user to previous page.
In this article we will see how to get the URL of the previous page from which you were redirected to the New page.
We have to use UrlReferrer  property of Request  object to access previous page url. The UrlReferrer property is used to get the information about the URL of the client's previous request that linked to the current URL.
Please follow the below steps for the requirement given above.
Step1: Creating a new Website: PrvsPageDemo
a.     Open Visual Web Developer or VisualStudio 2008.
b.     On the File menu, click New Website, In the Dialog box that appears Under VisualStudioInstalled templates  select ASP.NET Website.
c.     Then Type the location (Foldername) where you want to create your website such as D:\ PrvsPageDemo in the second location box.
d.     Select the language you want to create website in. Let us select C#.
e.     Click ok , This will create a website with the name PrvsPageDemo. This website by default contains a Webpage named Default.aspx.
f.        Add 2 more Webpages namely Management.aspx  and Development.aspx page to the website. (Go to View tab ->click on SolutionExplorer -> In the Solution Explorer window right click on the Project name(PrvsPageDemo) -> Select Add New Item -> In the name Box: type Management.aspx and then click on Add. Similarly add Development.aspx page.)
Step 2: In the Default Page Source view, add the following code under form tag.
<div>
    This is Default Page.<br /><br />
    <asp:Button runat="server" ID="btnBack" Text="Back" onclick="btnBack_Click" />
    </div>
Go to Default.aspx.cs file and add the following code as below:
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.
        }
    }

Here, In the Default.aspx page we have added some static text and a Back Button.
In the Default.aspx.cs page, In the PageLoad event , in !IsPostBack section, we are Saving the value of the Request.UrlReferrer in viewState which is the Previous page URL and using the same in the Back Button click event handler.
In the Button click , We are first checking if ViewState contains Previous page url. If the ViewState contains Previous Page URL then we are using the viewstate information to redirect the user back to the previous page.

Step 3: In the Management.aspx page under form tag, add the following code:
<div>
    This is management Page.<br /><br />
        <asp:Button ID="btnDefault" runat="server" Text="Go to Default Page"
            onclick="btnDefault_Click" />
    </div>
In the code-behind file (Management.aspx.cs ) add the below code:
protected void btnDefault_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
Here, When you click the button, User is redirected to Default.aspx page as written in the btnDefault_click event handler of the button.

Step 4: In the Development.aspx page under form tag, add the following code:
<div>
    This is Development Page.<br /><br />
        <asp:Button ID="btnDefault" runat="server" Text="Go to Default Page"
            onclick="btnDefault_Click" />
    </div>
In the code-behind file (Development.aspx.cs ) add the below code:
protected void btnDefault_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
Here, When you click the button, User is redirected to Default.aspx page.

Step 5:   Press CTRL + F5 to execute the Default page.
The Webpage (Default.aspx page) appears in the web browser. Click the Back button nothing happens.
Now view the Management.aspx page and then click on the button on this page which will redirect you to Default Page. When you click on Back button on Default.aspx page now. It will redirect you to Management.aspx page. Similarly try accessing Development.aspx page.
Conclusion: Using Request.UrlReferrer we can get the Previous page url of the current request. Previous Page information is available in the Request.UrlReferrer  property only if user is redirected to the current page from some other page and is available in the !Page.IsPostBack of Page Load event of the form.Here we are using ViewState because it can be used if you want to store the values on postback to the same form. ViewState is used for retaining values between multiple requests for the same page. This is the default method that the page uses to preserve page and control property values between round trips.



No comments:

Post a Comment