Post data to other Web pages with ASP.NET 2.0
Wednesday, May 16, 2007 12:59 PM
ASP.NET 2.0's PostBackUrl attribute allows you to designate where a Web form and its data is sent when submitted. Tony Patton explains how you can make use of this new Web tool.
Standard HTML forms allow you to post or send data to another page or application via the method attribute of the form element. In ASP.NET 1.x, Web pages utilize the postback mechanism where the page's data is submitted back to the page itself. ASP.NET 2.0 provides additional functionality by allowing a Web page to be submitted to another page. This week, I examine this new feature.
The old approach
I want to take a minute to cover the older HTML approach to
gain perspective. The HTML form element contains the action attribute to
designate what server side resource will handle the submitted data. The
following code provides an example.
<html>
<head><title>Sample HTML form</title></head>
<body>
<form name="frmSample" method="post" action="target_url">
<input type="text" name="fullname" id="fullname" />
<input type="button" name="Submit" value="submit" />
</form>
</body></html>
The value entered in the text field (fullname) is submitted to the page or program specified in the form element's action attribute. For ASP.NET developers, standard HTML forms are seldom, if ever, used.
ASP.NET developers have plenty of options when faced with the task of passing values from page to page. This includes session variables, cookies, querystring variables, caching, and even Server.Transfer, but ASP.NET 2.0 adds another option.
ASP.NET 2.0 alternative
When designing ASP.NET 2.0, Microsoft recognized the need to
easily cross post data between Web forms. With that in mind, the PostBackUrl attribute was added to the ASP.NET button
control. It allows you to designate where the form and its data are sent when
submitted (via the URL assigned to the PostBackUrl
attribute). Basically, cross posting is a client side transfer that uses
JavaScript behind the scenes.
The ASP.NET Web form in Listing A has two text fields (name and e-mail address) and a button to submit the data. The PostBackUrl attribute of the submit button is assigned to another Web form, so the data is sent to that page when the form is submitted. Note: The form is set up to post the data when it is submitted via the method attribute of the form element, but this is unnecessary since all cross postbacks utilize post by design.
Working with previous pages
The IsPostBack property of an
ASP.NET Page object is not triggered when it is loaded via a cross postback call. However, a new property called PreviousPage allows you to access and work with pages
utilizing cross postbacks.
When a cross page request occurs, the PreviousPage property of the current Page class holds a reference to the page that caused the postback. If the page is not the target of a cross-page posting, or if the pages are in different applications, the PreviousPage property is not initialized.
You can determine if a page is being loaded as a result of a cross postback by checking the PreviousPage object. A null value indicates a regular load, while a null value signals a cross postback. Also, the Page class contains a new method called IsCrossPagePostBack to specifically determine whether the page is loading as the result of a cross postback.
Once you determine that a cross postback has occurred, you can access controls on the calling page via the PreviousPage object's FindControl method. The code in Listing B is the second page in our example; it is called by the page in the previous listing.
The page determines if it is being called via a cross postback. If so, the values from the calling page are accessed using the FindControl method and converting the controls returned by this method to TextBox controls and displaying their Text properties.
You can convert the entire PreviousPage object to the type of the page initiating the cross postback. This approach allows you to access the public properties and methods of a page. Before I offer an example of this technique, I must rewrite the first example to include public properties. Listing C is the first listing with two properties added to give access to field values.
Now that the properties have been established, you can easily access them. One caveat is that the Page class' PreviousPage object must be converted to the appropriate type to properly work with the properties. This is accomplished by converting it to the necessary page level object.
Listing D illustrates this point by referencing the calling page in the top of the page, so it can be used in the code. Once it is referenced, the actual VB.NET code converts the PreviousPage object to the appropriate type using the CType function. At this point, the properties may be used as the code demonstrates.
A note about the use of the IsValid method of the PreviousPage object in the previous listing: The IsValid property of a previous page allows you to ensure a page passed all validation tests before working with it.
Summary
Passing data values between Web pages has many applications,
including maintaining personal user information. Legacy Web solutions, like
using the querystring and cookies, allows you to pass
and maintain values, and you can easily direct one
page to another when submitted. ASP.NET 1.1 supported these solutions as well
as additional ones, but ASP.NET 2.0 addresses the issue head on by supporting
cross page postbacks. This makes it easy for one Web
page to process data from another. Take advantage of this new concept when you're working on your next
ASP.NET 2.0 application.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.




There are currently no comments for this post.