方法一:
WebForm1.aspx:
!--文本框-->
<asp:TextBox ID="TextBox1" runat="server" Width="147px"></asp:TextBox>
<br />
<!--按钮-->
<asp:Button ID="Button1" runat="server" Text="提交到Page2" PostBackUrl="~/WebForm2.aspx" />
WebForm2.aspx:
<!--lable用来显示WebForm1中文本框中输入的内容-->
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
WebForm2.aspx.cs:
if (PreviousPage != null) //有页面控件传到此页面
{
TextBox mytxb = PreviousPage.FindControl("Textbox1") as TextBox;//as用来防止与其他控件类型的混乱
if (mytxb != null)
{
this.Label1.Text = mytxb.Text;
}
}
PostBackUrl:传值到指定页。PreviousPage属性用来获取向当前页传输控件的页。FindControl获取控件的ID。
方法二:
WebForm1.aspx.cs:
在Page_Load之外,并且在类中,定义一个叫的UserName设置为只读属性
public partial class WebForm1 : System.Web.UI.Page
{
public string UserName
{
get { return this.TextBox1.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
WebForm2.aspx:
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
PreviousPageType:
@PreviousPageType指令是一个新指令,用于处理ASP.NET 提供的跨页面传送新功能。
@PreviousPageType允许ASP.NET页面处理应用程序中另一个页面的回送信息。
这个简单的指令只包含两个属性:TypeName和VirtualPath。
TypeName:设置回送时的派生类的名称。
WebForm2.aspx.cs:
WebForm1.aspx:
!--文本框-->
<asp:TextBox ID="TextBox1" runat="server" Width="147px"></asp:TextBox>
<br />
<!--按钮-->
<asp:Button ID="Button1" runat="server" Text="提交到Page2" PostBackUrl="~/WebForm2.aspx" />
WebForm2.aspx:
<!--lable用来显示WebForm1中文本框中输入的内容-->
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
WebForm2.aspx.cs:
if (PreviousPage != null) //有页面控件传到此页面
{
TextBox mytxb = PreviousPage.FindControl("Textbox1") as TextBox;//as用来防止与其他控件类型的混乱
if (mytxb != null)
{
this.Label1.Text = mytxb.Text;
}
}
PostBackUrl:传值到指定页。PreviousPage属性用来获取向当前页传输控件的页。FindControl获取控件的ID。
方法二:
WebForm1.aspx.cs:
在Page_Load之外,并且在类中,定义一个叫的UserName设置为只读属性
public partial class WebForm1 : System.Web.UI.Page
{
public string UserName
{
get { return this.TextBox1.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
WebForm2.aspx:
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
PreviousPageType:
@PreviousPageType指令是一个新指令,用于处理ASP.NET 提供的跨页面传送新功能。
@PreviousPageType允许ASP.NET页面处理应用程序中另一个页面的回送信息。
这个简单的指令只包含两个属性:TypeName和VirtualPath。
TypeName:设置回送时的派生类的名称。
VirtualPath:设置回送时所传送页面的地址。
this.Label1.Text = this.PreviousPage.UserName;
图片:
WebForm1.aspx:
WebForm1.aspx.cs:
WebForm2.aspx:
WebForm2.aspx.cs: