0

I have a textbox on a page but when i use

TextBox formTextBox = Page.FindControl(textBox) as TextBox;

it comes back null. Is there a way around this? I know the control is on the page but i cant find it.

Thanks

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Wesley Skeen
  • 7,977
  • 13
  • 42
  • 56
  • 1
    You know that the control is on the page, but **where**? Can you shouw your markup? Apart from that, are you using `MasterPages`? If this is true, have a look at my answer on another question: http://stackoverflow.com/a/8163964/284240 – Tim Schmelter Mar 01 '12 at 16:18
  • Is it in a content area of a master page? – Zachary Mar 01 '12 at 16:20
  • Yes, It is in a content area of a master page – Wesley Skeen Mar 01 '12 at 16:24
  • Here is a [MSDN link](http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx) on how to reference master page content. – Zachary Mar 01 '12 at 16:34

3 Answers3

4

If you're using MasterPages and this control is in a page sitting in a ContentPlaceholder, you cannot get the reference to the control via FindControl directly, since the only control in the page's ControlCollection is the MasterPage itself. That makes sense. You cannot guarantee an ID to be unique when the control is on the top level of a page with MasterPage, because other ContentPages might as well have a control with this ID and FindControl could today return another control than tomorrow.

If you look at the NamingContainer of the Control you want to find, you see that in case of a MasterPage it is the ContentPlaceHolder and in case of a "normal" Page it is the Page itself.

So you need to get a reference to the MasterPage's ContentPlaceholder first before you could find the control via FindControl:

Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

But why don't you simply reference your control directly? For example:

this.TextBox1.Text = "Hello World";

By the way, this is derived from my own answer on a similar question.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Put a place holder around the text box in the markup, like this:

<asp:PlaceHolder ID="MyPlaceHolder" runat="server>
  <asp:TextBox ID="MyTextBox" runat="server" />
</asp:PlaceHolder>

Then you can fin the text box using:

TextBox formTextBox = MyPlaceHolder.FindControl("MyTextBox") as TextBox;
Daniel
  • 1,044
  • 11
  • 10
0

One of two things is happening... either the control is not being found (this is the most likely) or it is not returning a TextBox object.

The thing to remember about FindControl is that it is NOT recursive... it will only look at the top-level child controls. So, if your text box is nested inside another control, it will not be found. you can read the MSDN docs here.

You may want to make your own version of FindControl that will search inside nested controls--implementing such a method is trivial and can be found easily using your google-foo

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • I'm sure a recursive finder already exists, he only need look for it. – jcolebrand Mar 01 '12 at 16:22
  • Hi, It is only neseted in a div. – Wesley Skeen Mar 01 '12 at 16:23
  • @jcolebrand other people have written such, but it does not exist in the API. you have to implement it yourself or borrow someone else's – Muad'Dib Mar 01 '12 at 16:24
  • @WesleySkeen then you'll need to search inside that div. Give it an id and runat=server (so an – jcolebrand Mar 01 '12 at 16:25
  • @jcolebrand: A div (or Panel) does not implement INamingContainer. – Tim Schmelter Mar 01 '12 at 16:26
  • @TimSchmelter ??? I could've sworn I've used it before for that ... MSDN! I must be thinking of a Content then, since they get translated into the subsumed parental CPH. Interesting. Either that, or I'm thinking of my UserControls. – jcolebrand Mar 01 '12 at 16:35
  • @TimSchmelter no, I grokked that. I just thought I had used one for that purpose before. Also, you probably didn't see my edit while typing your response. For future visitors wanting to see those INamingContainer objects: http://i.imgur.com/LtZnC.png – jcolebrand Mar 01 '12 at 16:41
  • @jcolebrand: What i wanted to say is, a NamingContainer ensures that all controls IDs are unique in this container. It creates unique IDs in itself. Then you need to use FindControl on this container control to find your nested control. But since Panel does not implements it, you can be sure that it would not help to FindControl on a Panel. The problem here is that the page is inside of a ContentPlaceholder of a MasterPage. So the NamingContainer of it is not the page(and not the div/panel) but the ContentPlaceholder. http://stackoverflow.com/a/9520061/284240 – Tim Schmelter Mar 01 '12 at 16:42