I want to pass the value of TextBox1 from one page to another using PostBackUrl. So here is the code for the first page.
<form id="form1" runat="server">
<div>
<h2>Working With the Previous Page Object</h2>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Default7.aspx"/>
</div>
</form>
Now, here is the code for the page that retrieves the value from the First Page:
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if(previousPage != null)
{
Label1.Text = ((TextBox)previousPage.FindControl("TextBox1")).Text;
}
}
Off course I have inserted a Label called "Label1" on the page that retrieves the value of TextBox1 from the First Page.
I have seen lots of Tutorials doing exactly the same thing, but it just does not work for me, I do not know why. Any help is welcomed.
Probably here, the
FindControl()is not recognizing any control namedTextBox1which is the ID that you gave in the design code.You can try using the full Unique ID of that control in the
FindControl()like this :This Unique ID would be the ID generated at runtime when control is rendered into HTML. You can read it from the HTML source using inspect element in the browser.
As an other option, you can also try finding the
ContentPlaceHolderfirst and then finding theTextBoxwith the given ID inside it.Hope this helps.