Cross Page Posting is a new feature in ASP.NET 2.0. By using this feature we can submit a form (say crosspage1.aspx) along with all its control values into another page (say crosspage2.aspx).
We have to set PostBackUrl property on any button controls ( Button, ImageButton and LinkButton).
There are couple of ways of getting control values of the source page into the target page.
- Using FindControl Method
Crosspage1.aspx
<form id="form1" runat="server"> <div> <asp:Label ID="lblUName" runat="server" Text="Name">asp:Label> <asp:TextBox ID="txtUName" runat="server">asp:TextBox> <br/> <asp:Label ID="lblPWD" runat="server" Text="Password">asp:Label> <asp:TextBox ID="txtPWD" runat="server">asp:TextBox> <br/> <asp:Button ID="btnGo" runat="server" OnClick="btnGo_Click" Text="GO" PostBackUrl="crosspage2.aspx" /> div> form>
The above code is for crosspage1.aspx where PostBackUrl property is set for a button named btnGo. This property takes a string value which points to the location of the file to which this page should post. In this case, it is page crosspage2.aspx. This means that the crosspage2.aspx now receives the postback and all the values contained in crosspage1.aspx page controls.
Now, access the controls at Crosspage2.aspx
Crosspage2.aspx
Response.Write("Name:"+ ((TextBox)(PreviousPage.FindControl("txtUName"))).Text
+"
");
Response.Write("Password:" +((TextBox)(PreviousPage.FindControl("txtPWD"))).Text
+"
");
Another way of exposing the control values from the crosspage1 to crosspage2 is to create a property of control/controls.
Using Control Property
Write below properties in crosspage1.aspx.
Crosspage1.aspx
public string Name
{
get
{
return txtUName.Text;
}
}
public string Password
{
get
{
return txtPWD.Text;
}
}
In order to work with the properties described in crosspage1, PreviousPageType directive is required in the posting page that is in crosspage2.aspx.
<%@ PreviousPageType VirtualPath="crosspage1.aspx"%>
Use the properties of crosspage1.aspx in crosspage2.aspx
Response.Write("Name:" + PreviousPage.Name + "
");
Response.Write("Password:" + PreviousPage.Password);
We can use the Server.Transfer method to move between pages, however, the URL does not change. The cross page posting feature in ASP.NET 2.0 allows us to do a normal postback to a different page in the application. We can then access the values of server controls of the source page in the target page. Using the cross page posting feature we can also post back to any page and pages in another application. However, the PreviousPage property will return null if we are posting pages to another application.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.