Click here to Skip to main content
Click here to Skip to main content
Go to top

IsCrossPagePostBack: Cross page PostBack

, 29 Jul 2011
Rate this:
Please Sign up or sign in to vote.
How to make use of the IsCrossPagePostBack property.

Introduction

The last few days, I was working on a module in my web application. While writing code, I came across one property IsCrossPagePostBack of the Page class. I have seen this earlier but never tried it. So I thought of exploring it and here I am sharing my findings with you all.

Let’s start from beginning. When we submit a page, the entire form data with hidden fields, including the ViewState is posted to the server. You must be familiar with the IsPostBack property. It is false when the page is loaded the first time but returns true if a postback takes place. Whenever we click a server side control like a button, linkbuttons, etc., or any other control with autopostback set to true, all the data including hidden fields, ViewState etc., is posted to the server and available for processing at the code-behind.

But have you ever used IsCrossPagePostBack? No?

By default, the buttons or server side events post the entire page’s data to itself. But we can post the data of one page to another page/URL as well. Posting the data of one page to another means we can access data/controls of the previous page as well. The previous page can be accessed by using the Page.PreviousPage property. To find any control from the last page, you need to know the ID of that control and then find it using the FindControl method of the Page and then cast it appropriately. This feature was introduced in ASP.NET 2.0.

Note: Do have a null check called before using the FindControl method of the previous page.

How do we implement crosspage postback in ASP.NET? Every control that implements IButtonControl has a property PostBackURl. You just need to provide the the URL of your next page. The rest will be taken care of by ASP.NET.

Now let us jump to the demo part. I have two pages FirstPage.aspx and SecondPage.aspx. I also have a user control SimpleCalculator.ascx that I have used on FirstPage.aspx. In this control, I have two textboxes and a few buttons and a label. I am just doing simple arithmetic operations on these button clicks. And also a few controls are directly on the webpage. I will try to access these controls on the next page, that is SecondPage.aspx. So my user control is like:

My FirstPage is like:

ASPX:
<form id="form1" runat="server">
    <div>
        <uc1:SimpleCalculator ID="ucSimpleCalculator" runat="server" />
        <asp:Label ID="lblHello" runat="server" Text="Say Hello"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Cross Page Post Back"
           PostBackUrl="~/SecondPage.aspx"/>
    </div>
</form>

I have not written any code in the code behind of the page. Now my user control:

ACSX:
<table>
    <tr>
        <td><asp:TextBox ID="op1" runat="server"></asp:TextBox></td>
        <td><asp:TextBox ID="op2" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td align="right">
            <asp:Button ID="btnAdd" runat="server" Text="+"
            onclick="btnAdd_Click" />
            <asp:Button ID="btnSubstract" runat="server" Text="-"
                    onclick="btnSubstract_Click" />
        </td>
        <td>
            <asp:Button ID="btnMultiplication" runat="server" Text="*"
                    onclick="btnMultiplication_Click" />
            <asp:Button ID="btnDivision" runat="server" Text="/"
            onclick="btnDivision_Click" />
        </td>
    </tr>

    <tr>
        <td colspan="2" align="center">
            <asp:Label ID="lblResult" runat="server" Text="Result"></asp:Label>
        </td>
    </tr>
</table>

Here the code-behind of the user control implements the methods of the button and displays the result. (Complete demo is attached with this blog.) I will demonstrate here accessing the ASP.NET controls which are on the user control and other controls which are directly on the page.

Let us now see the SecondPage.

ASPX:
<form id="form1" runat="server">
    <div>
        <asp:Label ID="lblText" runat="server" Text="Result from Previous Page:"></asp:Label>
        <asp:Label ID="lblSecondResult" runat="server" Text="Label"></asp:Label>
    </div>
</form>

Now the code-behind:

public partial class SecondPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        // Page.PreviousPage returns the Previous Page.
        // Here checking Previous Page and
        // IsCrossPagePostBack property of Previous Pgae
        // Caveat: You cannot check here IsCrossPagePostBack
        // of current page. It will always return false.
        if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostBack)
        {
            //Here getting the user control of Previous Page
            var ctrl = Page.PreviousPage.FindControl("ucSimpleCalculator");

            //getting op1 textbox of user control
            var tbop1 = ctrl.FindControl("op1");

            // Getting the result label of user control
            // from Previous Page and assigning the value on this page
            Label lblResult = (Label)(ctrl.FindControl("lblResult"));
            lblSecondResult.Text = lblResult.Text;

            //Similarly we can find any other control
            //of the previous page : Say to get the Hello label here
            Label lblHello = (Label)Page.PreviousPage.FindControl("lblHello");
        }

    }
}

The code is properly commented and self explanatory.

One caveat, on SecondPage, you cannot check the IsCrossPagePostback of the current page; you need to check it for the PreviosPage, because the previous page initiated the crosspage postback. At one point of time, it may sound confusing, but it is right.

Now if you have some public properties in your last page, how will you access them?

Actually, Page.PreviousPage gives you the page as an Object type. In this case, you would not be able to access the properties directly. You need a strongly typed object of the previous page.

You can get this by adding a directive in the ASPX of the second page:

<%@ PreviousPageType VirtualPath="~/FirstPage.aspx" %>

After adding this, Page.PreviousPage will give you the strongly typed object and you can access the properties directly.

Is there any other way to access the PreviousPage?

Yes, there is one other way, using the Server.Transfer method. When we use this method to navigate from one page to another, it allows the HTTPContext to make the previous page available. But this is not cross-page postback, so you will not get IsCrossPagePostback as true. One major difference is, as we know, there are several limitations with Server.Transfer, like URL does not get changed.

I hope this post throws some light on cross-page postback and you all like it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brij
Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.
 
He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.
 
He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.
 
He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.
 
Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.


Visit his Blog: Brij's arena of .NET
 
Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 2 PinmemberMember 438224830-Mar-12 0:55 
GeneralMy vote of 5 PinmemberMonjurul Habib31-Jul-11 5:37 
GeneralRe: My vote of 5 PinmvpBrij1-Aug-11 7:42 
GeneralMy vote of 5 PinmvpKunal_Chowdhury29-Jul-11 20:09 
GeneralRe: My vote of 5 PinmvpBrij29-Jul-11 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 29 Jul 2011
Article Copyright 2011 by Brij
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid