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 inASP.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
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;
}
}
<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
}
No comments:
Post a Comment