Enter Cross-page Postbacks
In ASP.NET 2.0, the cross-page postback feature lets you configure page controls that implement the new IButtonControl interface to post to a different target page from the originating web form. Like Response.Redirect
, cross-page postbacks are a client-based transfer mechanismbut like Server.Transfer
, the target web page can (usually) access the control values of the source page. To set up a cross-page postback, you specify the target URL in the PostBackUrl
property of the source web page.
Implementing Cross-page Postbacks
This section discusses how to implement cross-page postbacks in an ASP.NET 2.0 application. If you're not already thoroughly familiar with ASP.NET postback operations, you can find out more here
To begin with, assume that you have two web pagesa source and a target. The source web page initiates the cross-page postback operation using a Button control. You first have to set the PostBackUrl
property for the button to the target web page's URL. Incidentally, all web controls that implement the System.Web.UI.WebControls.IButtonControl interface have the cross-page postback feature enabled by default. The following code snippet shows how to set the property.
<asp:Button ID="btnSubmit" runat="server"
PostBackUrl="~/target.aspx" text = "Post to a target page"/>
When you set the PostBackUrl
, generating HTML similar to the following:
<input type="submit" name="btnSubmit" value="Post to target Page"
new WebForm_PostBackOptions("btnSubmit", "", false, "",
"Target.aspx", false, false))" id="btnSubmit" />
With that HTML in place, when a user clicks the button, the browser will submit to the target URL (Target.aspx
) rather than to the source URL.
Retrieving Source Control Values from the Target Page
ASP.NET 2.0 provides a new property called PreviousPage
that returns a reference to the type of the source page whenever a cross-page post back occurs in your web application. Note that this property contains null
(it's not initialized) when the source and the target web pages are in different applications. Also note that when the target page accesses the PreviousPage
property to retrieve the control values of the source page, the ASP.NET runtime loads and executes the source page. That fires the ProcessChildRequest
event handler. Moreover, it also fires the Page_Init
and any other source page button click events.
So that you can avoid doing all that work accidentally, it's best to check the IsCrossPostBack
Boolean property to determine whether a cross-page postback has actually occurred; the value is true
if the target page was requested by a cross-page postback has actually occurred. The value is false
if control reached the target web page another way (such as a normal request, a Response.Redirect
, or a Server.Transfer
). Here's an example that illustrates how to use this property:
if ( PreviousPage.IsCrossPagePostBack)
property works for both Server.Transfer
and cross-page postbacks in ASP.NET 2.0. In ASP.NET 2.0, you can use the PreviousPageProperty
after a Server.Transfer
operation to retrieve the control values of the source page from within the target page. Here's an example:
protected void Redirect_Click(object sender, EventArgs e)
The receiving web page can now retrieve the control values of the sender web page as shown in the code snippet below:
protected void Page_Load(object sender, EventArgs e)
if (PreviousPage != null)
TextBox txtBox = (TextBox)
if (textBox != null)
string userName = textBox.Text;
Notice that the preceding code has to cast the txtUserName
control to a TextBox type to access the value. There's a better way, as you'll see.