Tuesday, October 4, 2011

Cross Page Posting

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
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.