I am trying to pass the value of a variable from one page to another using cross page post-back using this code:
on page1:
<asp:TextBox ID="changepwd" runat="server"></asp:TextBox>
<asp:Button ID="ChangePassword" runat="server" Text="Change Password"
PostBackUrl="~/Page2.aspx" />
I have assigned its value at runtime from the database in the cs file as: changepwd.Text = dataSet.Tables[0].Rows[0]["empPassword"].ToString();
On page 2: In page load event:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txt = (TextBox)PreviousPage.FindControl("changepwd");
TextBox1.Text = txt.Text;
}
}
but I don't get the value from the previous page. i am getting the value as null
. On page1 I am getting the value correctly from the database but it is not being passed onto page 2. Can you please tell me why?
Hope its helping u:
Page 1:
<asp:TextBox ID="changepwd" runat="server"></asp:TextBox>
<asp:Button ID="btnChangePassword" runat="server" Text="Change Password"
PostBackUrl="~/Page2.aspx" />
Page 1 Behind Code:
public TextBox ChangePassword
{
get
{
return changepwd;
}
}
Page 2: define this at the page header:
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
Page 2 Behind Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
{
TextBox1.Text = PreviousPage.ChangePassword.Text;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.