Click here to Skip to main content
15,878,959 members
Articles / Web Development / ASP.NET
Article

Cross Page Posting in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.31/5 (7 votes)
13 May 20064 min read 114.8K   31   6
Explains the concept of cross page posting in ASP.NET 2.0

Cross page posting in ASP.NET 2.0<o:p>

<o:p> 

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

Platform: ASP.NET 2.0, Visual Studio 2005<o:p>

Level: Intermediate<o:p>

<o:p> 

Introduction<o:p>

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.<o:p>

<o:p> 

Cross Page Postback in ASP.NET 1.1<o:p>

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.<o:p>

<o:p> 

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.<o:p>

<o:p> 

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.<o:p>

<o:p> 

Cross Page postback in ASP.NET 2.0<o:p>

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.<o:p>

<o:p> 

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.<o:p>

<o:p> 

Response.Write(<o:p>

((TextBox)this.PreviousPage.FindControl("MyTextBox")).Text)<o:p>

<o:p> 

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).<o:p>

<o:p> 

Login.aspx<o:p>

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

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

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

         PostBackUrl="UserAuthenticate.aspx" /><o:p>

<o:p> 

Expose the following properties in code behind file <o:p>

public TextBox UserName<o:p>

{<o:p>

    get<o:p>

    {<o:p>

        return TextBoxUserName;<o:p>

    }<o:p>

}<o:p>

<o:p> 

public TextBox Password<o:p>

{<o:p>

    get<o:p>

    {<o:p>

        return TextBoxPassword;<o:p>

    }<o:p>

}

 

<o:p><o:p>

UserAuthenticate.aspx<o:p>

<o:p>

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

<script runat="server">    <o:p>

protected void Page_Load(object sender, System.EventArgs e)<o:p>

{<o:p>

    String username = PreviousPage.UserName.Text;<o:p>

    String password = PreviousPage.Password.Text;<o:p>

    // Authenticate username/password<o:p>

}<o:p>

<o:p> 

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.<o:p>

 

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)<o:p>

{<o:p>

    String username = PreviousPage.UserName.Text;<o:p>

    String password = PreviousPage.Password.Text;<o:p>

    // Authenticate username/password<o:p>

}


Internals of Cross page posting<o:p>

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.<o:p>

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

<o:p> 

Conclusion<o:p>

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.<o:p>

<o:p> 

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


Written By
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 Pin
Avdhesh kumar24-Mar-13 19:27
Avdhesh kumar24-Mar-13 19:27 
GeneralMy vote of 5 Pin
CPpayal7-Dec-10 19:49
CPpayal7-Dec-10 19:49 
QuestionIsCrossPagePostBack property of which page Source or Target ? Pin
shan aspdotnet16-Sep-09 19:59
shan aspdotnet16-Sep-09 19:59 
Generalits much more simpler Pin
taminha22-Feb-07 22:35
taminha22-Feb-07 22:35 
GeneralA problem with Cross Page Posting in ASP.NET Webpages Pin
Hussein Madani Ghomi18-Aug-06 20:22
Hussein Madani Ghomi18-Aug-06 20:22 
GeneralRe: A problem with Cross Page Posting in ASP.NET Webpages Pin
Prakash Zele29-Aug-07 13:29
Prakash Zele29-Aug-07 13:29 

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

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