Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Cross-Page Postback in ASP.NET 2.0


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].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.