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 pagenot the current page as usual in the ASP.NET programming model.
To enable this feature, you dont 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 controlsButton, 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. Heres an example of the code youre 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 doesnt see any of the controls defined on the ASPX posted page. The controls, in fact, are protected and theres 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 pages 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].