且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FindControl已不能按预期工作?

更新时间:2023-02-24 17:21:40

我发现回答

只有控制从 TemplateControl 继承,它实现了作INamingContainer 接口。

和明显< D​​IV RUNAT服务器> 是不是其中之一。

即。 用户控件

 公共类用户控件:TemplateControl,IAttributeAccessor,INonBindingContainer,作INamingContainer,IUserControlDesignerAccessor
{...}

即。 的ContentPlaceHolder

 公共类的ContentPlaceHolder:控制,INonBindingContainer,作INamingContainer
{...}

在这里我可以看到所有这些控件:

FindControl已不能按预期工作?

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

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 :