First: The user control code-behind:
Next, the User control
[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 : System.Web.UI.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;
}
}
protected override void CreateChildControls()
{
if (this.m_Items == null)
{
this.m_Items = new ListItemCollection();
}
if (m_Items.Count>0) {
foreach (ListItem item in m_Items)
{
_ComboBoxControl.Items.Add(item);
}
}
}
public override void DataBind()
{
CreateChildControls();
ChildControlsCreated = true;
_ComboBoxControl.DataBind();
base.DataBind();
}
}
And finally the aspx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ComboBoxControl.ascx.cs" Inherits="ComboBoxControl" %>
<asp:DropDownList ID="_ComboBoxControl" runat="server">
</asp:DropDownList>
<uc1:ComboBoxControl ID="ComboBoxControl1" runat="server">
<Items>
<asp:ListItem Value="Value1" Text="Text1" />
<asp:ListItem Value="Value2" Text="Text2" />
<asp:ListItem Value="Value3" Text="Text3" />
<asp:ListItem Value="Value4" Text="Text4" />
</Items>
</uc1:ComboBoxControl>
Credit to: http://developers.de/blogs/damir_dobric/archive/2007/03/30/Implementing-Custom-UserControl-with-ListItem-collection.aspx