Sometimes you will have a trivial requirement to create just simple ASP.NET user control (control derived from UserControl), which should have a property of a collection type.
Following example illustrates such control:
public partial class ComboBoxControl : UserControl { protected void Page_Load(object sender, EventArgs e) { } private ListItemCollection m_Items; public virtual ListItemCollection Items { get { if (this.m_Items == null) { this.m_Items = new ListItemCollection(); } return this.m_Items; } } } |
Now, assume you would like to be able to supply elements of the collection directly in designer as shown in following code snippet:
<aco:ComboBoxControl ID="ComboBoxControl1" Style="left: 120px; top: 220px; width: 172px" CssClass="Field" runat="server" TabIndex="77"> <asp:ListItem Value="Value1" Text="Text1" /> <asp:ListItem Value="Value2" Text="Text1" /> <asp:ListItem Value="Value3" Text="Text1" /> <asp:ListItem Value="Value4" Text="Text1" /> </aco:ComboBoxControl> |
Unfortunately, after compiling following error can be noticed in Visual Studio:
"Content is not allowed between opening and closing tags for element 'ComboBoxControl'."
Moreover, if the application is started following error occurs:
"Type 'ASP.comboboxcontrol_ascx' does not have a public property named 'ListItem'"
To solve the problem just few attributes have to be set on the control's class and the property Items as shown in the next example:
[DefaultEvent("SelectedIndexChanged"), Designer("System.Web.UI.Design.WebControls.ListControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ParseChildren(true, "Items"), ControlValueProperty("SelectedValue"), DataBindingHandler("System.Web.UI.Design.WebControls.ListControlDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] public partial class ComboBoxControl : UserControl { protected void Page_Load(object sender, EventArgs e) { } private ListItemCollection m_Items;
[DefaultValue((string)null), MergableProperty(false), PersistenceMode (PersistenceMode.InnerDefaultProperty), Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public virtual ListItemCollection Items { get { if (this.m_Items == null) { this.m_Items = new ListItemCollection(); } return this.m_Items; } } }
|
Posted
Mar 30 2007, 11:43 PM
by
Damir Dobric