Cross-Page Posting
The postback architecture of ASP.NET is undoubtedly good, but it can be confusing to many traditional ASP programmers. The problem people have is not with automatically posting back to the same page but with not being able to specify another page to post to. There are reasons both for (security) and against (big pages) posting to another page and many people used Server.Transfer to move between pages. Content from the posting form was accessible only by ensuring the posting page was strongly typed or by storing it before posting (such as in the Items collection of the page). The biggest problem with Server.Transfer is that the original URL still shows in the browser.ASP.NET 2.0 has made cross-page posting easier by allowing button controls to indicate the form they are posting to. This actually sets the action attribute on the form, but specifying this attribute manually will not work because it continues to be ignored. Security issues regarding the ViewState are not relevant; the post will instruct the receiving page to ignore it. The previous page can be accessed with the new page property PreviousPage.
Posting to Another Page
Let's consider two pagesPage1 (Page1.aspx) needs to post to Page2 (Page2.aspx). Page1 could look like the code shown in Listing 9.1.
Listing 9.1 Posting to Another Page
<form runat="server">
This is the first page
<p />
Please select a country:
<asp:DropDownList id="Country" runat="server">
<asp:ListItem text="USA" value="0" />
<asp:ListItem text="Canada" value="1" />
<asp:ListItem text="UK" value="2" />
</asp:DropDownList>
<p />
<asp:Button id="Button1"
Text="This button just posts back to itself"
onClick="btn_click" runat="server" />
<br />
<asp:Button id="Button2"
Text="This button posts to another page"
PostTargetUrl="Page2.aspx" runat="server" />
</form>
The important section is that containing Button2, where you see the PostTargetUrl attribute, which is set to the page being posted to. When the button is clicked, instead of posting back to the same page, the postback is redirected to the requested page, which can then access controls from Page1. However, controls on a page are protected, so they either have to be exposed on the previous page as a Public Property or accessed via FindControl. For example, we could add the following to Page1 to expose the list of countries:
Public ReadOnly Property SelectedCountry() As DropDownList
Get
Return Country
End Get
End Property
The exposed control doesn't have to be ReadOnly, but it works well for this example.
Accessing the Previous Page
Once the postback has been completed, you are now in the second page, and with the addition of the PreviousPage property, Page2.aspx now has the capability to access content from Page1.aspx. However, by default, PreviousPage is not strongly typed and will therefore be of type Page. To strongly type the page, use the PreviousPage directive:
<%@ PreviousPage VirtualPath="page_name.aspx" %>
or
<%@ PreviousPage TypeName="type" %>
Only one of the attributes can be used at a time.Now the exposed controls can be accessed easily. For example, consider Page2 (see Listing 9.2).
Listing 9.2 Using a Strongly Typed Page
<%@ Page Language="VB" %>
<%@ PreviousPage VirtualPath="Page1.aspx" %>
<script runat="server">
Public Sub Page_Load()
PrevMessage.Text = "On the previous page you selected: " & _
PreviousPage.SelectedCountry.SelectedItem.Text
End Sub
</script>
<form runat="server">
This is the second page
<p />
<asp:Label id="PrevMessage" runat="server" />
</form>
Here you can see that the PreviousPage directive indicates that Page1.aspx is the previous pagethis ensures that the PreviousPage property will be strongly typed. From Page1.aspx the DropDownList is exposed via the SelectedCountry property.You can have only one PreviousPage directive on a page, although this doesn't prevent multiple pages from cross-posting to a single page. However, setting the PreviousPage directive will mean that all pages will be strongly typed as the same type. Under these circumstances it's best to not use the PreviousPage directive and instead access late-bound controls.
Transferring to Another Page in Code
Transferring execution to another page can also be achieved with Server .Transfer, and with ASP.NET 2.0 this has another overloaded method:
Server.Transfer(IHttpHandler, preserveForm)
The parameters are:IHttpHandler indicates an object that implements the IHttpHandler interface (such as the Page object) and thus the PreviousPage property.preserveForm is a Boolean value indicating whether or not the form contents (i.e., ViewState) should be preserved across the transfer.
Detecting Cross-Page Posting
The question that naturally arises out of the preceding code is what happens if Page2 is accessed directly, without having been posted to from Page1.aspx, or perhaps if it posts back to itself. As well as the PreviousPage property, there is an IsCrossPagePostBack property, which indicates whether or not a page is participating in a cross-page postback. This property is True only for Page1 during its second instantiation, when accessed via the PreviousPage property of Page2. The following lists show what properties are set under what circumstances.
| Page1 Posting Back to Itself | |
| Page1.IsPostBack | True |
| Page1.PreviousPage | null (Nothing in Visual Basic .NET) |
| Page1.IsCrossPagePostBack | False |
| Page1 Cross-Posting to Page2 | |
| Page2.PreviousPage | Reference to Page1 |
| Page1.IsCrossPagePostBack | True |
| Page1.IsPostBack | True |
| Page2.IsPostBack | False |
| Page2.IsCrossPagePostBack | False |
| Page1 Transfers to Page2 with Server.Transfer | |
| Page2.PreviousPage | Reference to Page1 |
| Page1.IsCrossPagePostBack | False |
| Page1.IsPostBack | False |
| Page2.IsPostBack | False |
| Page2.IsCrossPagePostBack | False |
The Page Life Cycle
It is important to understand the life cycle of the pages when posting across pages. The following list indicates what happens when Page1 posts to Page2, and in what order:Page1 cross-posts to Page2 via a button with its PostTargetUrl property set.ViewState from Page1 is stored by Page2 but ignored.The PreviousPage property is accessed in Page2.Page1 is instantiated and the stored ViewState from Page1 is applied.Page1 executes up to the OnLoadComplete event. For more details on which events will be fired, see Table 9.9 later in the chapter.
Understanding this life cycle ensures that you realize the implications of using cross-page posting. For example, posting from a page with a large amount of ViewState means the ViewState is stored and then reposted when the PreviousPage is accessed.
• Table of ContentsA First Look at ASP.NET v. 2.0By
Publisher: Addison WesleyPub Date: October 23, 2003ISBN: 0-321-22896-0Pages: 528
"This book provides a first look at the new major release of ASP.NET. The authors have worked closely with the ASP.NET team at Microsoft to make sure that this book is authoritative, accurate, and informative. Anyone using ASP.NET will find a wealth of useful info on the next version."
-Scott Guthrie, Product Unit Manager, Web Platforms and Tools Team, Microsoft Corporation
A First Look at ASP.NET v. 2.0 systematically prepares you for Microsoft''''s ASP.NET 2.0 Web development platform, showing how to use it to reduce development time and costs, improve the performance of Web applications and services, simplify deployment and management, and extend your applications to mobile clients.
Drawing on extraordinary insider access to Microsoft''''s ASP.NET development team, three renowned ASP.NET experts cover every key platform enhancement, from Master Page templates for standardizing look and feel to improved security and data- access controls. They go far beyond already published white papers and previews, presenting detailed development techniques, best practices, and sample code that will give you a running start with ASP.NET 2.0.
This book''''s detailed coverage includes:
Streamlining database integration with "code-free" data binding and ASP.NET 2.0''''s new data source controls
Using Master Pages to improve your control over application look and feel
Strengthening security with membership providers, security server controls, cookieless forms authentication, and the new Role Manager
Simplifying personalization with ASP.NET 2.0 ThemesBuilding fast, flexible portals with Web Parts and the new Portal Framework
Delivering more effective mobile device support-with less complexity
Previewing new ASP.NET 2.0 controls and changes in existing controls
Using Microsoft''''s powerful new .NET and built-in administration tools
Whether you''''re already developing with ASP.NET, or preparing to migrate from ASP, A First Look at ASP.NET v. 2.0 gives you complete information for assessing ASP.NET 2.0-and making the most of it.