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)
{
if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostBack)
{
var ctrl = Page.PreviousPage.FindControl("ucSimpleCalculator");
var tbop1 = ctrl.FindControl("op1");
Label lblResult = (Label)(ctrl.FindControl("lblResult"));
lblSecondResult.Text = lblResult.Text;
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.