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

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.

y default, an ASP.NET web form posts back to itself whenever a postback operation occurs. That behavior wasn't terribly common in web pages before ASP.NET appeared, and isn't always what you want to have happen: What if you want to post a web form to another web page in the application? ASP.NET made that fairly difficult in ASP.NET 1.0, but ASP.NET 2.0 makes developers lives easier in this regard by adding a new feature called "cross-page postbacks" that allows a web page to post its data back to a different web page. In cross-page postbacks, the page that initiates the postback is called the source page and the page to which the client posts is called the target page.

Conveniently, the target page can still retrieve any control values posted by the source web page in a cross-page postback operation. In other words, from a development point of view, you can process the posted data in much the same way you would process any normal ASP.NET postback.

The new feature means ASP.NET 2.0 developers now have three techniques for transferring processing from one web page to another in an application: Response.Redirect, Server.Transfer, and the new cross-page postback features. You're probably familiar with the first two, so I'll review them only briefly, and then concentrate on how to use the cross-page postback feature, showing how it works and how it differs from the Response.Redirect and Server.Transfer techniques.

The Response.Redirect Method
The Response.Redirect method is by far the simplest method of redirecting processing from a source page to a different destination or target page in the same or even across different applications. When the web server receives a request for redirection, it sends a response header to the client that causes the client to send a new request back to the server. In other words, a redirect causes two request/response cycles: one for the original and one for the new redirected request.

ASP.NET makes redirects easy. The following code snippet illustrates how to use the Response.Redirect method:

   protected void Redirect_Click(object sender, EventArgs e)
Note that the redirect request is a GET request, meaning that you can't post data from the original page via the Response.Redirect command. You can work around that to pass data from the source to the target by using query strings in the Response.Redirect method call as shown below:

   protected void Redirect_Click(object sender, EventArgs e)
      Response.Redirect("menu.aspx?userName=" + UserName.Text));
The preceding example passes a query string as a parameter to the Response.Redirect method along with the target URL. The target can now use code such as the following to retrieve the source's data as shown below:

   protected void Page_Load(object sender, EventArgs e)
      string userName = Request["userName"];
      //Other code
The Server.Transfer Method
Instead of relying on the client to make a request to a new page, Server.Transfer is a server-side redirection technique that simply changes the code's "focus" on the web server to a new page. Server.Transfer is far more efficient than Response.Redirect when the target page resides on the same web server, because it avoids the extra roundtrip and uses only server resources for the redirection. Note that it has a side effect—the URL shown in the web browser does not change either—meaning clients may think they posted their data to one page while actually posting it to a different page. In most cases, that's not a problem, but it can make debugging more difficult.

Server.Transfer also preserves the HttpContext of the initiating page; therefore the target page can access the control values of the source page. Specifically, you can use the FormsCollection property to retrieve the source control values from the target page when using the Server.Transfer technique. First, make sure you use the overloaded Server.Transfer method, which accepts two parameters—the target URL, and a Boolean preserveForm value which tells the server to preserve the query string and any control values from the source page. Set the preserveForm parameter to true in the source web page as shown below:

Then, from the target page, to retrieve the value of a textbox control called txtUserName for example, use the following code:

   object obj = Request.Form["txtUserName"];
Response.Redirect vs. Server.Transfer
Because Response.Redirect requires an extra round trip, you should avoid it for high-volume web sites due to associated performance and scalability issues. However, this is the only technique that you can use to redirect from one web server to another. In contrast, Server.Transfer is more efficient, but can transfer execution only to a different web page on the same server. In essence, you'd use Server.Transfer to eliminate unnecessary roundtrips when both the source and the target web page reside on the same server (they can be in different ASP.NET applications on that server), and use Response.Redirect when you need to redirect to a different server.

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