我想最好創建一個自定義的控制是這樣的:標準子控件的自定義控件 - 爲什麼它們的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:該代碼適用於我的目的。不知道爲什麼。我必須做出一些改變才能實現這一目標。只有第二個例子有效。 –