IsCrossPagePostBack : Cross page PostBack

Download sample from here

Last few days, I was working on one of my module in my web application. During writing code, I came accorss one property IsCrassPagePostBack of Page Class. I have seen this earlier but never tried to dig. So I thought of exploring it and here I am sharing my findings with you all.

First, Let’s start from beginning. We know when we submit a page, entire form data with hidden fields, including view state is posted to the server. One must be using IsPostBack property a lot. It is false when page loaded first time but returns true if any postback takes place. Whenever we click any server side control like button, linkbuttons etc or any other control with autopostback true. All the data including hidden field, View State etc.. is posted to the server and available for processing at codebehind.

But did you ever use IsCrossPagePostBack?
No!!

Let’s explore this

By default, the buttons or server side events posts the entire page’s data to itself. But we can post the data of one page to another page/URL as well. So as we are posting the data of one page to another , means we can access the data/controls of previous page as well. The Previous page can be accessed by  Page.PreviousPage property. So to find any control from the last page, you need to know the Id of that control 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 before using the FindControl method of previous page.
So how to 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. Rest things will be taken care by ASP.NET.

So now jump to Demo part

Here I have two pages FirstPage.aspx and SecondPage.aspx.I have also a user control SimpleCalculator.ascx, that I have used on FirstPage.aspx. In this control, I have two textboxes and few buttons and a label. I am just doing simple arithmetic operation on these button clicks. And also 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

And 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 on 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 this user control is just implementing the methods of button and display the result.
(Complete demo is attached with this blog)
So I will demonstrate here to access the asp.net controls which are on this user control and other  controls which are directly lying on the page.

Now Lets see 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 Lets see 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 already properly commented and self explanatory.

One caveat, On secondPage, you cannot check the IsCrossPagePostback of the current page, you need to check it of the PreviosPage’s one. Because the Previous page has initiated the crosspage postback. At one point of time it may sound confusing , but it is right.

Now if you have some public properies in your last page. Then How will you access it?

Actrually the Page.PreviousPage  gives you the page as Object type. So in this case you would not be able to access the properties directly. You need a strongly typed object of Previous Page.

So you can get this, by adding a direcive at aspx of Second Page as

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

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

Also Is there any other way to access the PreviousPage ?
Yes there is one other way, Using Server.Transfer method. When we use this method to navigate from one page to other, it allows the HTTPContext to make available the previouspage. But this is not crosspagepostback, so you dont get here iscrosspagepostback, true. One major difference, as we know there are sevral limitations with Serv er.Transfer like URL does not get changed

Lets hope this post throws some light, on Cross Page postback and you all have like it.

Advertisement

2 thoughts on “IsCrossPagePostBack : Cross page PostBack

  1. Hi, I’ve seen you have some experience with ChangeKey command… In Outlook 2011 for Mac sync service with the Exchange server I’m experiencing repeatedly an error code -19946, with the notice: “Could not synchronize record …” and the explanation “ChangeKey is required for this operation”. Do you have any idea where is this bug coming from?

    Regards,

    Wojtek.

    • Hi Wojtek,

      From you question, I am not able to get whether you have developed some custom service for Syncing or using some tool for that. I have experience in Exchange Web Services (EWS). Now as the problem about Change Key, every Item in a MailBox (Whether Mailitem, CalenderItem, ContactItem etc.) is identified by an object of Class ItemIdType. Which has actually tow proprties:
      Id and ChangeKey. So whenever we want get a Item in readonly, we can fetch it by providing Id only. But if we need to do any modification on an Item (like update, delete etc) we need both Id and ChangeKey else it will throw an error as you mentioned.
      Hope it clarifies a bit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s