我想最好创建一个自定义的控制是这样的:标准子控件的自定义控件 - 为什么它们的ID为null?
<foo:Frobnicate runat="server">
<DoStuffWithThese>
<asp:TextBox runat="server" ID="fizzbot" />
</DoStuffWithThese>
<!-- other controls can be in the wrapper but outside the DoSomeStuffWithThese collection -->
<asp:DropDownList runat="server" ID="othercontrol" />
</foo:Frobnicate>
,但我会在必要时解决此:
<foo:Frobnicate runat="server">
<DoStuffWithThese>
<asp:TextBox runat="server" ID="fizzbot" />
</DoStuffWithThese>
<AndOtherStuffWithThese>
<asp:DropDownList runat="server" ID="othercontrol" />
</AndOtherStuffWithThese>
</foo:Frobnicate>
我可以访问控件的代码隐藏OK(在第二,不理想的例子),但由于某些原因,他们的ID(我需要)是NULL。
这里是隐藏代码:
[ParseChildren(true)]
public class Frobnicate : WebControl {
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<WebControl> DoStuffWithThese { get; set; }
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<WebControl> AndOtherStuffWithThese { get; set; }
public override OnLoad(EventArgs e) {
base.OnLoad(e);
foreach(Control currentControl in DoStuffWithThese) {
// the control can be accessed (e.g. I can see it's a TextBox, etc.
// but currentControl.ID == null here -- why? :(
}
}
有谁知道这是为什么?更重要的是,我如何解决这个问题,以便我可以使用这些格式之一获得自定义控件,并且能够访问这些ID?
不,我可以既不得到下拉或文本框ID =无论是在自定义控件,我可以访问它,但自定义控件的OnLoad ID为空。将代码示例更新为包含容器中的runat服务器 – Whisker
Hi Whisker:该代码适用于我的目的。不知道为什么。我必须做出一些改变才能实现这一目标。只有第二个例子有效。 –