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 3

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.

A Better Approach: Using PreviousPageType
The PreviousPageType property provides strongly typed access to a source web page in a cross-page postback operation, letting you retrieve control values from the source page without any typecasting overhead. The code snippets below illustrate how you can use this property.

In the source page you might write:

   <asp:Textbox ID="txtUserName" Runat="server" />
   <asp:Textbox ID="txtPassword" Runat="server" />
   <asp:Button ID="Submit" Runat="server" Text="Login"
   PostBackUrl="Menu.aspx" />
Note that clicking the button redirects the user to a "Menu.aspx" target page. The target page can retrieve the username and password values using this code:

   <%@ PreviousPageType VirtualPath="~/Login.aspx" %>
   <script runat="server">    
   protected void Page_Load(object sender, System.EventArgs e)
       String userName = PreviousPage.txtUserName.Text;
       String password = PreviousPage.txtPassword.Text;
       //Other code
In the preceding code, the PreviousPageType property returns a strongly typed reference to the source web page, eliminating the need to cast.

ViewState Preserved
For cross-page postbacks, ASP.NET 2.0 embeds a hidden input field named __POSTBACK that contains the ViewState information of the source web page—provided the source page contains a server control with a non-null PostBackUrl property value. The target page can use that __POSTBACK information to retrieve the ViewState information of the source web page as shown below:

   if(PreviousPage!=null && PreviousPage.IsCrossPagePostBack && 
       TextBox txtBox = PreviousPage.FindControl("txtUserName");
The preceding code checks to ensure that the PreviousPage property is not null. Incidentally, the PreviousPage property contains null if the target page is not in the same application. The IsCrossPagePostBack property will be true only if processing reached the target page due to a cross-page postback operation.

The Cross Page Postback feature, one of the most powerful features in ASP.NET 2.0, allows a web page to postback to another web page and still enable retrieval of the values of the server controls of the source web page from within the target web page seamlessly.

Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date