0

I have this structure :

Page
 |
 +---Ascx
      |
      +---<div  runat="server">
                    |
                    +---<asp:button>

I already know that Findcontrol doesn't work recursively .

However when I pressed the button and went to debug and wrote :

this.FindControl("btn1") it did find the "button"

But

  • this= the ascx
  • There is a div runat server which wraps the button

So how did it find it ? According to the definition , it doesn't suppose to work.

Msdn :

Control.FindControl --> The method does not search throughout a hierarchy of controls within controls

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

2 Answers2

1

The asp.net have a different tree struct than the actually DOM of html page.

The controls on the same page are like on the same branch of the tree. So all the controls on the same page can be found by searching the page. The controls that are inside some custom control are also all together.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • if div would have been another user control , it would'nt find it.( and I know that it is a different tree obviously :-) , it's just that according to the definition - it is control under control – Royi Namir Mar 03 '13 at 19:12
  • @RoyiNamir Yes this is what I say you, the tree of the asp.net is not the same as the html tree. Imaging them as modules. Each module have their branch on the tree. One module is the page, one other is a custom control. asp.net works with some kind of modules that work together to render the page. – Aristos Mar 03 '13 at 19:13
  • So which server controls open a new "module"( as you say) ? – Royi Namir Mar 03 '13 at 19:15
0

I found the answer.

Only controls which inherits from TemplateControl, which implements the INamingContainer interface.

and obviously <div runat server> is not one of them.

i.e. UserControl :

public class UserControl : TemplateControl, IAttributeAccessor, INonBindingContainer, INamingContainer, IUserControlDesignerAccessor
{...}

i.e. ContentPlaceHolder:

public class ContentPlaceHolder : Control, INonBindingContainer, INamingContainer
{...} 

and here I can see all those controls :

enter image description here

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792