デザイン時の CompositeControl の挙動
なんかおかしい。
↓の Items プロパティでは、内部コントロールを返すために EnsureChildControls メソッドを実行しているのだけれど、そうするとデザイン時に正しく処理がされないようで RadioButtonList 内の各項目が表示されない。なぜか、CreateChildControls が2回呼ばれているみたい。
実行時には問題無し。
プロパティの中で EnsureChildControls メソッドを呼ばないようにして、RadioButtonList の生成をコンストラクタとかで済ますようにすれば回避できるが…。
MyList.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.ComponentModel; namespace Sample.Controls { [ParseChildren(true, "Items")] [PersistChildren(false)] public class MyList : CompositeControl { private RadioButtonList _radioButtonList; [MergableProperty(false)] [PersistenceMode(PersistenceMode.InnerDefaultProperty)] public ListItemCollection Items { get { this.EnsureChildControls(); return this._radioButtonList.Items; } } protected override void CreateChildControls() { HtmlGenericControl _fieldset = new HtmlGenericControl("fieldset") { Controls = { (this._radioButtonList = new RadioButtonList()) } }; this.Controls.Add(this._fieldset); } } }
Default.aspx (1)
<%@ Register Assembly="Sample" Namespace="Sample.Controls" TagPrefix="my" %>
Default.aspx (2)
<my:MyList runat="server" ID="_myList1"> <asp:ListItem Text="ほげ" Value="Hoge" /> <asp:ListItem Text="ふが" Value="Fuga" /> <asp:ListItem Text="ぴよ" Value="Piyo" /> </my:MyList>