Cross-Page Posting in ASP.NET 2.0, Visual Studio 2005...
Cross-page posting enables you to submit a form and have this form and the control values post themselves to
another page.
By: Mathieu Cupryk
Date: December 18, 2004
Need Posting to Another Page? Cross-page posting enables you to submit a form for instance CrosssPage1.aspx and
have this form and the control values post themselves to another page CrosssPage2.aspx. The trick is ASP.NET 2.0
has made cross-page posting easier by allowing button controls to indicate the form they are posting to with
setting the action attribute on the form. Now, on CrosssPage2.aspx the previous page can be accessed via a
property by the name of PreviousPage. The following example will illustrate the process. Enjoy!
CrossPage1.aspx
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table>
<tr>
<td>Enter some Text:</td>
<td><asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Enter more Text:</td>
<td><asp:TextBox ID="TextBox2" Runat="server"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:LinkButton ID="LinkButton1" Runat="server" PostBackUrl="CrossPage2.aspx">Submit Page</asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
CrossPage2.aspx
<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="CrossPage1.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
// If posted from CrossPage1.aspx to CrossPage2.aspx...
if (PreviousPage.IsCrossPagePostBack)
{
// Retrieve the textbox values from FindControl
TextBox TextBox1;
TextBox TextBox2;
TextBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
TextBox2 = (TextBox)PreviousPage.FindControl("TextBox2");
Response.Write(TextBox1.Text+"<br>"+TextBox2.Text+"<br>");
}
else
{
Response.Redirect ("CrossPage1.aspx");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Page2</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:Label ID="Label1" Runat="server" EnableViewState="False"></asp:Label> <br />
<br />
<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="crosspage1.aspx">Back</asp:HyperLink>
</div>
</form>
</body>
</html>
|