我正在尝试使用此代码使用跨页回发将变量的值从一个页面传递到另一个页面:
第1页:
<asp:TextBox ID="changepwd" runat="server"></asp:TextBox>
<asp:Button ID="ChangePassword" runat="server" Text="Change Password"
PostBackUrl="~/Page2.aspx" />
我已经在运行时从cs文件中的数据库中分配了它的值:
changepwd.Text = dataSet.Tables[0].Rows[0]["empPassword"].ToString();
第2页: 在页面加载事件中:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txt = (TextBox)PreviousPage.FindControl("changepwd");
TextBox1.Text = txt.Text;
}
}
但我没有从上一页获得价值。我得到的值为null
。
在第1页上,我从数据库中正确地获取了值,但它没有被传递到第2页。你能告诉我为什么吗?
答案 0 :(得分:0)
希望它能帮到你:
第1页:
<asp:TextBox ID="changepwd" runat="server"></asp:TextBox>
<asp:Button ID="btnChangePassword" runat="server" Text="Change Password"
PostBackUrl="~/Page2.aspx" />
Page 1幕后代码:
public TextBox ChangePassword
{
get
{
return changepwd;
}
}
第2页: 在页眉中定义:
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
Page 2幕后代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
{
TextBox1.Text = PreviousPage.ChangePassword.Text;
}
}