Crosspage posting is a feature which is difficult to achieve in ASP.NET 1.0/1.1 and is available in ASP.Net 3.0 and above.
Crosspage posting enables you to submit a page(.aspx) and have this form and all the control values post themselves to another page.
Any page ehich is created in 1.0/1.1 simply posted to itself and hndled the control values within the page instance.The pages’ first request and any postback can be differentiated with the .IsPostback property.
But if the developer still wanted to post to a different page and deal with the first pages set of values on that page,which can be achieved by the cross page postback feature,which is quite simple.
Here I will explain you the two simple ways to achieve cross page posting.
Solution 1:
In the Example given,I have a Page where a user can enter some text and I have two buttons. First button saves the control values in the same page where as the second button submits it to a different page(Page 2)
Page1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="createword.Page1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="label1" runat="server" Text="Enter Something"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
<script runat="server">
</script>
</div>
<asp:Button ID="Button2" runat="server" Text="Save to next page" PostBackUrl="Page2.aspx" />
</form>
</body>
</html>
The two button properties are quite interesting.First button has the onclick event,so it submits to the same page.The second button has the PostbackURL property,which points to the second page(page2).This means Page2 recives a postback and all values contained in Page1.
Page2.aspx
In Page 2 ,we can access the previous page controls by using the Findcontrol() property.
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtPrevPage = (TextBox)PreviousPage.FindControl("TextBox1");
Label1.Text = "Your Entry in Prev.Page :-" + txtPrevPage.Text.Trim();
}
Solution2:
In solution 2, I will explain another way of exposing control from Page1 to Page 2
Page1.aspx.cs
In Page1,we can create a Property for the control,as shown below.
public TextBox txtPrevPage
{
get
{
return TextBox1;
}
}
Page2.aspx
To be able to work with the property from the previous page we have to strongly type the PreviousPage property using the @PreviousPage directive with the use of VirtualPath attribute.Through this we can access the previous page Properties.
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
By giving the PreviouPage in .aspx page of Page2 as shown above ,in the Page load event,we can call the property value of Page1.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Your Entry in Prev.Page :-" + PreviousPage.txtPrevPage.Text.Trim();
}
As you can see,working with CrossPageposting is quite simple.We can determine whether the request is coming from page1 or someone just hit the page2 directly is also easy.this can be achieved by using the isCrossPagePostBack() property.The below code snippet shows the very same.
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "Cross Page Post Back";
}
else
{
Label1.Text = " not a Cross Page Post Back";
}
}