1:Default页
default.aspx:
代码
<div>
Name:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Address:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
City:
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Original Page" />
<asp:Button ID="Button2" runat="server" PostBackUrl="~/Target.aspx" Text="Target Page" />
<asp:Button ID="Button3" runat="server" PostBackUrl="~/Target2.aspx" Text="Target2 Page" />
<asp:Button ID="Button4" runat="server" Text="Server Transfer Target" onclick="Button4_Click" />
<br />
<br />
<asp:Label ID="labName" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="labAddress" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="labCity" runat="server" Text="Label"></asp:Label>
<br />
</div>
default.aspx.cs
代码
protected void Page_Load(object sender, EventArgs e)
{
//If post back then save data to label controls.
if (IsPostBack)
{
SaveData();
}
}
/// <summary>
/// Transfer data to label controls.
/// </summary>
private void SaveData()
{
labName.Text = TextBox1.Text;
labAddress.Text = TextBox2.Text;
labCity.Text = TextBox3.Text;
}
/// <summary>
/// Public property for name textbox.
/// </summary>
public string Name
{
get { return TextBox1.Text; }
}
/// <summary>
/// Public property for address textbox.
/// </summary>
public string Address
{
get { return TextBox2.Text; }
}
/// <summary>
/// Public property for city textbox.
/// </summary>
public string City
{
get { return TextBox3.Text; }
}
/// <summary>
/// Actions when Transfer button is clicked.
/// </summary>
protected void Button4_Click(object sender, EventArgs e)
{
//Transfer postback processing to the Transfer.aspx page instead of Default.aspx page.
Server.Transfer("~/Transfer.aspx");
}
Target.aspx.cs
代码
protected void Page_Load(object sender, EventArgs e)
{
//Use IsCrossPagePostBack to determine if a postback occurred from the source page.
if (this.PreviousPage.IsCrossPagePostBack)
{
SaveData();
}
}
/// <summary>
/// Transfer data to label controls.
/// </summary>
private void SaveData()
{
//Get data using FindControl() to locate reference to the control on source page (Default.aspx).
TextBox text1 = this.PreviousPage.FindControl("TextBox1") as TextBox;
labName.Text = text1.Text;
TextBox text2 = this.PreviousPage.FindControl("TextBox2") as TextBox;
labAddress.Text = text2.Text;
TextBox text3 = this.PreviousPage.FindControl("TextBox3") as TextBox;
labCity.Text = text3.Text;
}
Target2.aspx.cs
代码
Transfer.aspx.cs
代码
protected void Page_Load(object sender, EventArgs e)
{
//Transfer from Default.aspx
SaveData();
}
/// <summary>
/// Transfer data to label controls.
/// </summary>
private void SaveData()
{
//Get data using FindControl() to locate reference to the control on source page (Default.aspx).
TextBox text1 = this.PreviousPage.FindControl("TextBox1") as TextBox;
labName.Text = text1.Text;
TextBox text2 = this.PreviousPage.FindControl("TextBox2") as TextBox;
labAddress.Text = text2.Text;
TextBox text3 = this.PreviousPage.FindControl("TextBox3") as TextBox;
labCity.Text = text3.Text;
}
来自:http://www.asp.net/general/videos/how-do-i-implement-a-cross-page-postback-in-aspnet