本人也是菜鸟一只,看了些提交到其他页的文章,觉得亲自试一下记忆比较深刻,于是乎行文于此。
源页 PostAnotherPage.aspx
目标页 PostAnotherPage2.aspx
PostAnotherPage.aspx
<
form id
=
"
form1
"
runat
=
"
server
"
target
=
"
_blank
"
>
<%-- 当提交到新页时,需要设置target,否则默认在本页打开提交 --%>
< div >
< asp:Label ID = " lblName " runat = " server " Text = " 姓名: " ></ asp:Label >
< asp:TextBox ID = " tbName " runat = " server " ></ asp:TextBox >< br />
< asp:Label ID = " lblEmail " runat = " server " Text = " 邮箱: " ></ asp:Label >
< asp:TextBox ID = " tbEmail " runat = " server " ></ asp:TextBox >< br />
< br />
< asp:DropDownList ID = " ddlFruit " runat = " server " >
< asp:ListItem Value = " 1 " > 香蕉 </ asp:ListItem >
< asp:ListItem Value = " 2 " > 苹果 </ asp:ListItem >
< asp:ListItem Value = " 3 " > 哈密瓜 </ asp:ListItem >
< asp:ListItem Value = " 4 " > 香橙 </ asp:ListItem >
< asp:ListItem Value = " 5 " > 西瓜 </ asp:ListItem >
</ asp:DropDownList >< br />
< asp:Button ID = " btnSubmit " runat = " server " Text = " 提交到PostAnotherPage2 " PostBackUrl = " ~/PostAnotherPage2.aspx " /></ div >
<%-- 提交按钮的PostBackUrl属性为提交的目的页 --%>
</ form >
<%-- 当提交到新页时,需要设置target,否则默认在本页打开提交 --%>
< div >
< asp:Label ID = " lblName " runat = " server " Text = " 姓名: " ></ asp:Label >
< asp:TextBox ID = " tbName " runat = " server " ></ asp:TextBox >< br />
< asp:Label ID = " lblEmail " runat = " server " Text = " 邮箱: " ></ asp:Label >
< asp:TextBox ID = " tbEmail " runat = " server " ></ asp:TextBox >< br />
< br />
< asp:DropDownList ID = " ddlFruit " runat = " server " >
< asp:ListItem Value = " 1 " > 香蕉 </ asp:ListItem >
< asp:ListItem Value = " 2 " > 苹果 </ asp:ListItem >
< asp:ListItem Value = " 3 " > 哈密瓜 </ asp:ListItem >
< asp:ListItem Value = " 4 " > 香橙 </ asp:ListItem >
< asp:ListItem Value = " 5 " > 西瓜 </ asp:ListItem >
</ asp:DropDownList >< br />
< asp:Button ID = " btnSubmit " runat = " server " Text = " 提交到PostAnotherPage2 " PostBackUrl = " ~/PostAnotherPage2.aspx " /></ div >
<%-- 提交按钮的PostBackUrl属性为提交的目的页 --%>
</ form >
PostAnotherPage.aspx.cs
public
string
UserName
... {
get
...{
return this.tbName.Text;
}
}
public string UserEmail
... {
get
...{
return this.tbEmail.Text;
}
}
// 把提交内容设置为公有属性(也可以通过其他方法获取源页属性,后面介绍)
... {
get
...{
return this.tbName.Text;
}
}
public string UserEmail
... {
get
...{
return this.tbEmail.Text;
}
}
// 把提交内容设置为公有属性(也可以通过其他方法获取源页属性,后面介绍)
PostAnotherPage2.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
PostAnotherPage2.aspx.cs
"
Inherits
=
"
PostAnotherPage2
"
%>
<% @ PreviousPageType VirtualPath = " ~/postAnotherPage.aspx " %>
<%-- 接收页必须设置此项,并指明源页 --%>
<! 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 >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
<%-- 前两者设置为公有属性 --%>
< asp:Label ID = " lblPreName " runat = " server " Text = " 前页传递的name: " ></ asp:Label >
< asp:Label ID = " Label1 " runat = " server " Text = " Label " Width = " 429px " ></ asp:Label >
< br />
< asp:Label ID = " lblPreEmail " runat = " server " Text = " 前页传递的Email: " ></ asp:Label >
< asp:Label ID = " Label3 " runat = " server " Text = " Label " Width = " 426px " ></ asp:Label >
< br />
< asp:Label ID = " lblFruit " runat = " server " Text = " 所选水果: " Width = " 130px " ></ asp:Label >
< asp:Label ID = " Label4 " runat = " server " Text = " Label " Width = " 428px " ></ asp:Label ></ div >
</ div >
</ form >
</ body >
</ html >
<% @ PreviousPageType VirtualPath = " ~/postAnotherPage.aspx " %>
<%-- 接收页必须设置此项,并指明源页 --%>
<! 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 >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
<%-- 前两者设置为公有属性 --%>
< asp:Label ID = " lblPreName " runat = " server " Text = " 前页传递的name: " ></ asp:Label >
< asp:Label ID = " Label1 " runat = " server " Text = " Label " Width = " 429px " ></ asp:Label >
< br />
< asp:Label ID = " lblPreEmail " runat = " server " Text = " 前页传递的Email: " ></ asp:Label >
< asp:Label ID = " Label3 " runat = " server " Text = " Label " Width = " 426px " ></ asp:Label >
< br />
< asp:Label ID = " lblFruit " runat = " server " Text = " 所选水果: " Width = " 130px " ></ asp:Label >
< asp:Label ID = " Label4 " runat = " server " Text = " Label " Width = " 428px " ></ asp:Label ></ div >
</ div >
</ form >
</ body >
</ html >
PostAnotherPage2.aspx
if
(PreviousPage
!=
null
)
// 判断该页的源页属性是否为空
... {
if (!IsCrossPagePostBack)
//判断该页是否为其他页提交而来
...{
//访问前页的公有属性
Label1.Text = PreviousPage.UserName;
Label3.Text = PreviousPage.UserEmail;
//访问前页的非公有属性,通过FindControl的方法即可
DropDownList fruit=(DropDownList)PreviousPage.FindControl("ddlFruit");
if (fruit != null)
...{
Label4.Text = string.Format("value:{0} text:{1}", fruit.SelectedValue, fruit.SelectedItem.Text);
}
}
}
// 判断该页的源页属性是否为空
... {
if (!IsCrossPagePostBack)
//判断该页是否为其他页提交而来
...{
//访问前页的公有属性
Label1.Text = PreviousPage.UserName;
Label3.Text = PreviousPage.UserEmail;
//访问前页的非公有属性,通过FindControl的方法即可
DropDownList fruit=(DropDownList)PreviousPage.FindControl("ddlFruit");
if (fruit != null)
...{
Label4.Text = string.Format("value:{0} text:{1}", fruit.SelectedValue, fruit.SelectedItem.Text);
}
}
}