Click here to Skip to main content
Click here to Skip to main content
Go to top

Cross Page Posting in ASP.NET 2.0

, 13 May 2006
Rate this:
Please Sign up or sign in to vote.
Explains the concept of cross page posting in ASP.NET 2.0

Cross page posting in ASP.NET 2.0

 

Intended Readers: Person having basic knowledge of ASP.NET 1.1, web applications etc.

Platform: ASP.NET 2.0, Visual Studio 2005

Level: Intermediate

 

Introduction

Almost every web application inputs data from the user and process it, in ASP.NET this process is called page posting. By default in ASP.NET 1.1 page can only post back to itself which some times is not the functionality developers wants. Although it’s possible in ASP.NET 1.1 that one page post backs to other page but this is quite expensive and not the recommended approach. Microsoft realize the importance of cross page posting in gives this functionality in ASP.NET 2.0 along with other features, so in this article I will first discuss the cross page in ASP.NET1.1 and then elaborate this concept in ASP.NET 2.0.

 

Cross Page Postback in ASP.NET 1.1

Asp.net 1.1 developers can achieve the functionality of cross page posting through Server.Transfer Method which preserver the HttpContext of current page before transferring to other page, because HttpContext is preserved you can access the source page’s items collection in target hence called cross page posting.

 

This functionality comes at price, the basic problem is that this transfer occurs at server level and current page must postback to itself before it can transfer to other page results extra processing overhead.

 

Security is another problem with the Server.Transfer method along with the viewstate issues; you can find more on MSDN and its knowledge base articles so let’s concentrate on ASP.NET 2.0.

 

Cross Page postback in ASP.NET 2.0

System.Web.UI.WebControls.IButtonControl interface contains a new property called PostBackUrl which points to the page to which the current page will postback, Button, ImageButton and LinkButton implements this interface and exposes the cross page postback functionality.

 

When user clicks the button the current page will postback to the specified page which can access the source page controls through Page.PreviousPage property which returns the reference of previous page, once got the reference of previous page you can use the FindControl method to get the reference of particular or you can expose public properties from source page to provide the type safe access i.e.

 

Response.Write(

((TextBox)this.PreviousPage.FindControl("MyTextBox")).Text)

 

In order to provide the strongly typed access to previous page, you can specify the previous page in PreviousPageType directive; since you have strongly typed reference of previous page you can now easily access its public members without any typecasting. Lets see how (here login page postbacks to UserAuthenticate page).

 

Login.aspx

<asp:Textbox ID="TextBoxUserName" Runat="server" />

<asp:Textbox ID="TextBoxPassword" Runat="server" />

<asp:Button ID="ButtonLogin" Runat="server" Text="Login"

         PostBackUrl="UserAuthenticate.aspx" />

 

Expose the following properties in code behind file

public TextBox UserName

{

    get

    {

        return TextBoxUserName;

    }

}

 

public TextBox Password

{

    get

    {

        return TextBoxPassword;

    }

}

 

UserAuthenticate.aspx

<%@ PreviousPageType VirtualPath="~/Login.aspx" %>

<script runat="server">   

protected void Page_Load(object sender, System.EventArgs e)

{

    String username = PreviousPage.UserName.Text;

    String password = PreviousPage.Password.Text;

    // Authenticate username/password

}

 

PreviousPageType directive specifies the virtual path of the source page for strongly typed access to the source page, but the problem with this approach is that you can only specify one previous page type and it cannot be used for pages which can be destination for multiple pages in this case only option left is late bind access using FindControl method or reflection.

 

It is not necessary that target page always executes as a postback, it can also be execute as stand alone, to determine this Page class contains new property called IsCrossPagePostBack it returns true when page is executing as a result of cross page postback, this behaves exactly same as IsPostBack property. So let’s modify the previous code to incorporate this


if (IsCrossPagePostBack)

{

    String username = PreviousPage.UserName.Text;

    String password = PreviousPage.Password.Text;

    // Authenticate username/password

}


Internals of Cross page posting

Cross page posting is basically a client side transfer from one page to other just like Response.Redirect, cross page posting can also span to multiple web application however PreviousPageType property will not be available but you can use Request.Form collection to get the values from the source page.

In the end the good news for Server.Transfer fans that PreviousPageType property of Page class is also works in Server.Transfer.

 

Conclusion

Cross page posting is yet another new feature introduced in ASP.NET 2.0, which eases the life of developers previously they have to use Server.Transfer which has its own advantages and disadvantages but now this is a part of ASP.NET which results flexibility and efficiency.

 

Your Comments and Feedbacks are always welcome

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

NasirAliKhan
Web Developer
United Arab Emirates United Arab Emirates
Iam Nasir Ali Khan, working a software architect in Dubai, works primarily on Microsoft Platform, MCTS certified for windows/web/distributed/mobile/enterprise applications.
Also teaches CS courses in a university in dubai.

Comments and Discussions

 
GeneralMy vote of 3 PinmemberAvdhesh kumar24-Mar-13 19:27 
GeneralMy vote of 5 PinmemberCPpayal7-Dec-10 19:49 
QuestionIsCrossPagePostBack property of which page Source or Target ? Pinmembershan_aspdotnet16-Sep-09 19:59 
Generalits much more simpler Pinmembertaminha22-Feb-07 22:35 
GeneralA problem with Cross Page Posting in ASP.NET Webpages PinmemberHussein Madani Ghomi18-Aug-06 20:22 
GeneralRe: A problem with Cross Page Posting in ASP.NET Webpages PinmemberPrakash Zele29-Aug-07 13:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 13 May 2006
Article Copyright 2006 by NasirAliKhan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid