Implementing Custom UserControl with ListItem collection

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

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
Filed under:

Comments

Pedram wrote re: Implementing Custom UserControl with ListItem collection
on 06-22-2007 13:56

As far as I know, all you need to solve this is to add the ParseChildren(true, "Items") attribute on the class.

Damir Dobric wrote re: Implementing Custom UserControl with ListItem collection
on 06-24-2007 23:22

But, how the list know what type of item should be parsed? The similar pattern can be found in the configuration system of .NET2.0

eric wrote re: Implementing Custom UserControl with ListItem collection
on 09-28-2007 21:15

this sample does not work as shown. i'm still getting the design time error ''Type 'ASP.comboboxcontrol_ascx' does not have a public property named 'ListItem

any suggestions?

Sleza4e wrote re: Implementing Custom UserControl with ListItem collection
on 01-17-2008 12:00

Took 1 to 1 as codesample, doesn't work too

vs2008

1 wrote re: Implementing Custom UserControl with ListItem collection
on 02-05-2008 7:21

1

techron wrote re: Implementing Custom UserControl with ListItem collection
on 02-10-2008 21:23

Works great. Here's a link to my implementation: techron.blogspot.com/.../creating-listitemcollection-template.html

Geoffrey Swenson wrote re: Implementing Custom UserControl with ListItem collection
on 04-04-2008 19:25

This was very helpful, especially techron's version. However, I will add that the following C# references were left out of his version:

using System.Text;

using System.Collections.Generic;

using System.ComponentModel;

using System.ComponentModel.Design;

using System.Drawing.Design;

Damir Dobric wrote re: Implementing Custom UserControl with ListItem collection
on 04-05-2008 23:23

Thanks for your support :)

shuihd wrote re: Implementing Custom UserControl with ListItem collection
on 05-06-2009 13:34

i have tested the code in vs 2008,but i  can not see it works even without the 'items' propety in the property window ,or in other case ,some message prompt that items can not be initialed ,could someone tells me why?

shuihd wrote re: Implementing Custom UserControl with ListItem collection
on 05-06-2009 13:40

does it work

Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies wrote Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies
on 11-24-2011 6:20

Pingback from  Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies

Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies wrote Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies
on 11-24-2011 6:20

Pingback from  Add ListItems to RadioButtonList via markup within a UserControl - Programmers Goodies

developers.de is a .Net Community Blog powered by daenet GmbH.