Cross Page Postbacks

Jun 27, 11:00 pm

Article Author: Kamran Qamar
.NET 3.5 Books

Introduction


A real problem in ASP.NET 1.x was that you could only post a page back to itself. This could be really frustrating, as it was awkward to do a simple task like writing a wizard in which you needed to pass data from previous page to next page, or sending someone to a different page on login. I was delighted to see this issue has been fixed in ASP.NET 2.0. The new cross-page postback feature allows you to post a page to another page. So I thought I’d use my first column to tell you a bit about how to take advantage of this feature.


Coding the Postbacks


Using the feature is straightforward and simple. Basically, you just set the PostBackUrl property, which is available on those web controls that implement the new System.Web.UI.WebControls.IButtonControl interface. The list of controls that implement this interface is available here. (http://beta.asp.net/QUICKSTART/util/classbrowser.aspx?assembly=System.Web,%20Version=2.0.0.0,%20Culture=neutral,%20PublicKeyToken=b03f5f7f11d50a3a&namespace=System.Web.UI.WebControls&class=IButtonControl)


Here is a step-by-step guide to implementing the cross-page postback using controls that implement the IButtonControl interface.


1. Create a Web Form and drop a Button control on it using the VS .NET designer. (The Button control implements the IButtonControl interface just mentioned.)


2. Set the button’s PostBackUrl property to the Web Form you want to post back.


That’s it. Here’s the generated markup:



<asp:Button ID="Button1" runat="server" 
   PostBackUrl="~/Page2.aspx" Text="Post to Page 2" />


How it Works under the Hood


When the PostBackUrl property of the IButtonControl is set, the ASP.NET framework binds the corresponding HTML element to new JavaScript function named WebForm_DoPostBackWithOptions . The corresponding HTML rendered by the ASP.NET 2.0 will look like this:



<input type="submit" name="Button1" value="Post to Page 2" 
   onclick="javascript:WebForm_DoPostBackWithOptions(
      new WebForm_PostBackOptions(
         "Button1", 
         "",
         false,
         "",
         "Page2.aspx", 
         false, 
         false
      )
   )" 
   id="Button1" />


Managing the ViewState


One problem with cross-page posting is the new page presumably needs the view state of the page posted from. This is at first sight problematic because the view state is page specific; it contains information, for example, about controls embedded on the particular page. ASP.NET 2.0 resolves this by embedding a hidden input field name, __POSTBACK . This field is embedded only when there is an IButtonControl on the page and its PostBackUrl property is set to a non-null value. This field contains the view state information of the poster page. To access the view state of the poster page, you can use the new PreviousPage property of the page:



Page poster = this.PreviousPage;


Then you can find any control from the previous page and read its state:



Label posterLabel = poster.findControl("myLabel");
string lbl = posterLabel.Text;


This cross-page postback feature also solves the problem of posting a Form to multiple pages, because each control, in theory, can point to different postback URL.


Note: You can post back to any page and pages in another application, too. But if you are posting pages to another application, the PreviousPage property will return null. This is a significant restriction, as it means that if you want to use the view state, you are confined, for example, to posting to pages in the same virtual directory. Even so, this is a highly welcome addition to the functionality of ASP.NET.

Founders at Work

Commenting is closed for this article.