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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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";
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0