3

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?

Bazzz
  • 26,427
  • 12
  • 52
  • 69
Seema
  • 107
  • 2
  • 5
  • 13
  • Is your textbox directly within page 1's naming container, or within some other control on the page? The [documentation](http://msdn.microsoft.com/en-us/library/ms178139(v=vs.100).aspx) says: *The FindControl method finds controls in the current naming container. If the control you are looking for is inside another control (typically, inside a template), you must first get a reference to the container and then search the container to find the control you want to get.* – Netricity May 19 '13 at 08:37
  • no it is not within any other control – Seema May 19 '13 at 11:25
  • are you using master pages? – Alok May 19 '13 at 11:50
  • 1
    Expose the textbox's text as a public property of page 1, then in page 2, cast PreviousPage to page 1's type and read the property. – Netricity May 19 '13 at 13:27
  • nope.. im not using master page – Seema May 19 '13 at 16:08

1 Answers1

0

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;
    }
}
Hooman
  • 1,775
  • 1
  • 16
  • 15