Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Cross-Page Postback in ASP.NET 2.0 – Page Model Part III

Posted by Ravi Varma Thumati on October 5, 2009

In ASP.NET 1.x, postbacks were required to post to the same page. Cross-page postbacks were not allowed. ASP.NET 2.0 adds the ability to post back to a different page via the IButtonControl interface. Any control that implements the new IButtonControl interface (Button, LinkButton, and ImageButton in addition to third-party custom controls) can take advantage of this new functionality via the use of the PostBackUrl attribute. The following code shows a Button control that posts back to a second page.

<asp:Button PostBackUrl="~/Default.aspx" runat="server" Text="Submit Report" />

When the page is posted back, the Page that initiates the postback is accessible via the PreviousPage property on the second page. This functionality is implemented via the new WebForm_DoPostBackWithOptions client-side function that ASP.NET 2.0 renders to the page when a control posts back to a different page. This JavaScript function is provided by the new WebResource.axd handler which emits script to the client.

More Details on Cross-Page Postbacks

Viewstate

You may have asked yourself already about what happens to the viewstate from the first page in a cross-page postback scenario. After all, any control that does not implement IPostBackDataHandler will persist its state via viewstate, so to have access to the properties of that control on the second page of a cross-page postback, you must have access to the viewstate for the page. ASP.NET 2.0 takes care of this scenario using a new hidden field in the second page called __PREVIOUSPAGE. The __PREVIOUSPAGE form field contains the viewstate for the first page so that you can have access to the properties of all controls in the second page.

Circumventing FindControl

We have to use the FindControl method to get a reference to the TextBox control on the first page. That method works well for that purpose, but FindControl is expensive and it requires writing additional code. Fortunately, ASP.NET 2.0 provides an alternative to FindControl for this purpose that will work in many scenarios. The PreviousPageType directive allows you to have a strongly-typed reference to the previous page by using either the TypeName or the VirtualPath attribute. The TypeName attribute allows you to specify the type of the previous page while the VirtualPath attribute allows you to refer to the previous page using a virtual path. After youve set the PreviousPageType directive, you must then expose the controls, etc. to which you want to allow access using public properties.

Lab1 Cross-Page Postback

In this lab, you will create an application that uses the new cross-page postback functionality of ASP.NET 2.0.

  1. Open Visual Studio 2005 and create a new ASP.NET Web site.
  2. Add a new Webform called page2.aspx.
  3. Open the Default.aspx in Design view and add a Button control and a TextBox control.
    1. Give the Button control an ID of SubmitButton and the TextBox control an ID of UserName.
    2. Set the PostBackUrl property of the Button to page2.aspx.
  4. Open page2.aspx in Source view.
  5. Add a @ PreviousPageType directive as shown below:
  6. Add the following code to the Page_Load of page2.aspx’s code-behind:
  7. Build the project by clicking on Build on the Build menu.
  8. Add the following code to the code-behind for Default.aspx:
  9. Change the Page_Load in page2.aspx to the following:
Response.Write(PreviousPage.UserName.Text);
public TextBox txtUserName {
    get { return this.UserName; }
}
Response.Write(PreviousPage.txtUserName.Text);

10.  Build the project.

11.  Run the project.

12.  Enter your name in the TextBox and click the button.

13.  Check the result?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: