ASP.NET用户控件不初始化子控件
在我的网页,我有以下几点:ASP.NET用户控件不初始化子控件
<aspe:UpdatePanel runat="server" ID="updatePanel">
<ContentTemplate>
<local:KeywordSelector runat="server" ID="ksKeywords" />
</ContentTemplate>
</aspe:UpdatePanel>
的KeywordSelector
控制是我在同一个组件定义和local
被映射到其命名空间的控制。
控制是由其他几个控件,并定义为这样:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="KeywordSelector.ascx.cs"
Inherits="Keywords.KeywordSelector" %>
,并有自己的定义为.designer.cs
文件成员相当多的服务器控件,所有。
然而,控制生命周期的任何部分在它有任何的子控件对象,也不产生HTML:
- 所有在
.designer.cs
文件中定义的成员都是null
。 - 致电
HasControls
返回false
。 - 致电
EnsureChildControls
什么都不做。 -
Controls
集合为空。
删除UpdatePanel
没有好处。我试图用一个新的UserControl
在一个干净的页面重现它,同样的事情发生。
我正在使用ASP.NET与.NET Framework 3.5 SP1和集成的Web服务器。
我在这里错过了什么?
更新#1:继罗布的评论,我看着OnInit
,发现UserControl
没有检测到有任何子控件。而且,CreateControlCollection
从不叫!
好吧,我已经找到了问题(S):
-
用户控制,而不是自定义控件必须在web.config中被注册一个接一个文件。这样做:
<add tagPrefix="local" tagName="KeywordSelector" src="~/KeywordSelector.ascx" />
代替:
<add tagPrefix="local" namespace="Keywords" assembly="Keywords" />
你不应该放置
WebControl
在同一目录中使用它的Control
。这是愚蠢的。 Read about it here。
感谢您的帮助。现在,如果我只能标记自己的答案为答案...
在我的情况的原因是ReSharper的7.1添加不正确@Register指令在ASPX的顶部 - 而不是此所需的行:
<%@ Register Src="~/Controls/Hello/Hello.ascx" TagName="Hello" TagPrefix="p" %>
我得到一个错误:
<%@ Register TagPrefix="p" Namespace="MyNamespace.WebApp.Controls" Assembly="MyApp.Web" %>
你动态加载此控件? – thismat 2008-10-28 14:08:21
没有。阅读有关这些... – 2008-10-28 14:11:26