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