Cross-Page Postback in ASP.NET 2.0

The upcoming cross-page posting feature lets you post the content of a form to another page, even to pages belonging to other applications


April 26, 2005
URL:http://www.drdobbs.com/cross-page-postback-in-aspnet-20/184406025

Cross-Page Postback in ASP.NET 2.0

Cross-page postbacks are a new feature of ASP.NET 2.0 that provides developers with an elegant mechanism to post the content of a form to another page—not the current page as usual in the ASP.NET programming model.

To enable this feature, you don’t have to use a different kind of form control or modify the structure of the page. Instead, you simply handle the click event on a bunch of button controls—Button, LinkButton, ImageButton, plus, in general, all controls that implement the IButtonControl interface.

Note that IButtonControl is a new interface to ASP.NET not available in version 1.1. However, if you happen to make a search for it through MSDN or the .NET Framework 1.1 documentation, you already find it defined. The namespace is different, though. In the .NET Framework 1.1, IButtonControl is supported only by Windows Forms controls.

Implementing cross-page postbacks requires only a couple of steps. First, you set the PostBackUrl property on server controls (typically, buttons) that can cause the postback. You will assign PostBackUrl with the target URL that will handle the posted data.

When the PostBackUrl property is set, the ASP.NET runtime binds the corresponding HTML element to a new JavaScript function. Instead of our old acquaintance __doPostback, it uses the new WebForm_DoPostBackWithOptions function. Here’s an example of the code you’re called to write or generate in Visual Studio .NET 2005:

<form runat="server">
   <asp:textbox runat="server" id="Data" />
   <asp:button runat="server" id="buttonPost"
            Text="Click" 
            PostTargetUrl="~/target.aspx" />
</form>

This button declaration will render to the following markup:

<input type="submit" name="buttonPost" id="buttonPost" 
     value="Click"
     onclick="javascript:WebForm_DoPostBackWithOptions(
        new WebForm_PostBackOptions("buttonPost", 
                                    "", 
                                    false, 
                                    "", 
                                    "target.aspx", 
                                    false, 
                                    false))" />

As a result, when the user clicks the button, the current form posts its content to the specified target page. What about the view state?

When the page contains a control that does cross-page posting, a new hidden field is created. The hidden field is named __PREVIOUSPAGE. The field contains the view state information to be used to serve the request. This view state information is used in lieu of the original view state of the page being posted to. You use the PreviousPage property to reference the posting page and all of its controls.

The PreviousPage property is of type Page and, as such, it doesn’t see any of the controls defined on the ASPX posted page. The controls, in fact, are protected and there’s no information available to make a cast to the real page type.

For this reason, to access a control on the original page, you have to resort to the following code:

TextBox txt = (TextBox) PreviousPage.FindControl("Data");

You use the page’s FindControl method to retrieve an object with the specified name and then start using its members.

The cross-page posting feature is similar, but not identical, to the action property of HTML forms. In the ASP.NET implementation, it enables you to post to pages belonging to other applications. In this case, though, the PreviousPage object returns null.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.