Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Solve Postback Hassles with Cross-Page Postbacks in ASP.NET 2.0 : Page 2

ASP.NET 2.0's new cross-page postback capability gives developers three choices to choose between when transferring server processing from one page to another—and they're all useful in different scenarios.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Enter Cross-page Postbacks
In ASP.NET 2.0, the cross-page postback feature lets you configure page controls that implement the new IButtonControl interface to post to a different target page from the originating web form. Like Response.Redirect, cross-page postbacks are a client-based transfer mechanism—but like Server.Transfer, the target web page can (usually) access the control values of the source page. To set up a cross-page postback, you specify the target URL in the PostBackUrl property of the source web page.

Implementing Cross-page Postbacks
This section discusses how to implement cross-page postbacks in an ASP.NET 2.0 application. If you're not already thoroughly familiar with ASP.NET postback operations, you can find out more here.

To begin with, assume that you have two web pages—a source and a target. The source web page initiates the cross-page postback operation using a Button control. You first have to set the PostBackUrl property for the button to the target web page's URL. Incidentally, all web controls that implement the System.Web.UI.WebControls.IButtonControl interface have the cross-page postback feature enabled by default. The following code snippet shows how to set the property.

<asp:Button ID="btnSubmit" runat="server" PostBackUrl="~/target.aspx" text = "Post to a target page"/>

When you set the PostBackUrl property, the ASP.NET framework binds the corresponding control to a new JavaScript function named WebForm_DoPostBackWithOptions, generating HTML similar to the following:

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

With that HTML in place, when a user clicks the button, the browser will submit to the target URL (Target.aspx) rather than to the source URL.

Retrieving Source Control Values from the Target Page
ASP.NET 2.0 provides a new property called PreviousPage that returns a reference to the type of the source page whenever a cross-page post back occurs in your web application. Note that this property contains null (it's not initialized) when the source and the target web pages are in different applications. Also note that when the target page accesses the PreviousPage property to retrieve the control values of the source page, the ASP.NET runtime loads and executes the source page. That fires the ProcessChildRequest event handler. Moreover, it also fires the Page_Init, Page_Load and any other source page button click events.

So that you can avoid doing all that work accidentally, it's best to check the IsCrossPostBack Boolean property to determine whether a cross-page postback has actually occurred; the value is true if the target page was requested by a cross-page postback has actually occurred. The value is false if control reached the target web page another way (such as a normal request, a Response.Redirect, or a Server.Transfer). Here's an example that illustrates how to use this property:

if ( PreviousPage.IsCrossPagePostBack) { //Some code }

The PreviousPage property works for both Server.Transfer and cross-page postbacks in ASP.NET 2.0. In ASP.NET 2.0, you can use the PreviousPageProperty after a Server.Transfer operation to retrieve the control values of the source page from within the target page. Here's an example:

protected void Redirect_Click(object sender, EventArgs e) { Server.Transfer("menu.aspx"); }

The receiving web page can now retrieve the control values of the sender web page as shown in the code snippet below:

protected void Page_Load(object sender, EventArgs e) { if (PreviousPage != null) { TextBox txtBox = (TextBox) PreviousPage.FindControl("txtUserName"); if (textBox != null) string userName = textBox.Text; //Other code } }

Notice that the preceding code has to cast the txtUserName control to a TextBox type to access the value. There's a better way, as you'll see.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date