I’m really struggling with this. The problem is only with the content pages.
I am trying to access a Text Box value from one content page (“Page1.aspx”) in another content page (“Page2.aspx”). I’m not sure whether it is relevant that they are the children of nested master pages, but I thought I’d throw it in.
Page1.aspx is a basic form with text boxes and a submit button. The text box in Page1.aspx is called “tbFirst”. The submit button has the following code:
<asp:Button ID="Button1" runat="server" Text="New Member Form" PostBackUrl="Page2.aspx"/>
Page2.aspx is a new form which should be populated with a textbox value from the previous page.
The second line show <%@ PreviousPageType VirtualPath=”~/Page1.aspx” %>
For testing purposes I am using a label (“lblResult”) to display my results.
Codebehind looks like this:
if (PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)PreviousPage.FindControl("tbFirst");
if (SourceTextBox != null)
{
lblResult.Text = SourceTextBox.Text;
}
else
{
lblResult.Text = "No text found";
}
}
else
{
lblResult.Text = "No Control found";
}
}
}
The problem is that the label text in Page2.aspx says “No text found”.
I think that’s all the relevant info. Anyone got any ideas? I’ve spent the whole afternoon trawling the forums and nothing I’ve tried works.
The
MasterPageis exactly what’s causing this issue. You cannot find a control on a page with MasterPage by usingPage.FindControl("ControlID"), because the Page is not the NamingContainer of the TextBox but theContentPlaceholder. The only control in the page’sControlCollectionwith MasterPage is the MasterPage itself.Reason: I’ve recently answered a question that describes this behaviour.
Here are some ways how you can access the TextBox from Page2:
You might have luck with following approach(the most direct
FindControl way):Another, better approach would be to provide a public property in
Page1that returnstbFirst.Text. Then you could access it in the following way fromPage2:You could also add the
Textas URL-Parameter, so that it’s not required thatPage2'sPreviousPageisPage1.Last but not least. If you use Server.Transer with
preserveFormset to tue, you would be able to retrieve the value of the original pageTextBoxcontrol by referencingRequest.Form("TbFirst").Note: I don’t recommend a recursive FindControl approach(starting from
MasterPage), because it would also hardwire both pages and would be