Ravindra Bagale
Ravindra Bagale

Reputation: 17665

cross page posting

i am just trying the example of cross page posting. i have added 1 textbox & 1 button to default.aspx page

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:Button ID="Button2" runat="server" Text="Button"  PostBackUrl="~/About.aspx"/>

i have added following code to code-behind file of about.aspx page

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            TextBox SourceTextBox =
                (TextBox)Page.PreviousPage.FindControl("TextBox1");
            if (SourceTextBox != null)
            {
                Label1.Text = SourceTextBox.Text;
            }
            else
                Label1.Text = "no value";
        }
        else
            Label1.Text = "no value from previous page";
    }

when i enters some text in textbox1 & clicks button, it goes to about.aspx but label shows value "no value", its not showing textbox1's text value, why this is not working properly?

Upvotes: 2

Views: 2048

Answers (1)

Aristos
Aristos

Reputation: 66641

If you have master page then the code Page.PreviousPage.FindControl("TextBox1"); not work because the TextBox1 is under the ContentPlaceHolder. and must first locate the ContentPlaceHolder. and then find the TextBox1

But there is an easiest way to get the value as:

Place this on the previous page:

public string TextFromBox1
{
    get
    {
        return TextBox1.Text;
    }
}

and on the redirect page declare what is the previous page on aspx as:

<%@ Reference Page ="~/PreviousPageName.aspx" %>

and on code behind get the value as:

if (Page.PreviousPage != null)
{
    if (Page.PreviousPage is PreviousPageClassName)
    {
        Label1.Text = ((PreviousPageClassName)Page.PreviousPage).TextFromBox1;
    }
    else
    {
        Label1.Text = "no value";
    }
}
else
    Label1.Text = "no value from previous page";

Upvotes: 3

Related Questions