My web site uses a master page, where I’ve placed two controls, a TextBox and an ImageButton, they are intended to be viewed and accessible on all content sites. Here’s the aspx code in Site.Master:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageBtn" runat="server" ImageUrl="~/Image.png"
PostBackUrl="~/Result.aspx"></asp:ImageButton>
Clicking the button should redirect the visitor to Result.aspx, which it does.
Result.aspx.cs has the following in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
TextBox txbx = this.Master.FindControl("TextBox1") as TextBox;
if (txbx != null)
{
Label1.Text = "Value: " + txbx.Text;
}
else
{
Label1.Text = "TextBox seems to be null";
}
}
The weird behavior appears when the image button is clicked the first time. The visitor gets this information from the page “Value: ” (i.e an empty string “”), even though a value was entered in TextBox1. The subsequent clicks presents the value(s) correctly, e.g “Value: SomeText”.
Why doesn’t the value “come along” the first time?
Is there a better way to ensure that?
- The visitor is redirected to Result.aspx AND
- that the value is “registered” and can be handled in Result.aspx.cs AND
- that a user will be redirected, if Result.aspx is entered without the use of the image buttons PostBackURL
I’ve tried IsPostBack but it seems to behave strangely when using a master page as previous page …
Very thankful for an answer!
Sincerely,
Mr Kay
If you are using
PostBackURLon your ImageButton on the master page and come from another page you must use:PreviousPage.Master.FindControl("TextBox1") as TextBox;If you are on the
PostBackURLpage and click your ImageButton, you need to useMaster.FinControl("TextBox1") as TextBox;(PreviousPage would be null in this case)
you could use something like this:
See: http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage(v=vs.100).aspx
at the Remarks section.