ASP.NET DropDownList Control

  • Used to display a drop-down list control that allows a single selection
  • Supports data binding:
    • Create a System.Collections.ArrayList object or other data source
    • Populate the ArrayList with items to display in the control
    • Use the Control.DataBind method to bind the data source to the DropDownList control

DropDownList Control Markup

<div style="font-weight: bold; width: 100%; clear: both">     

<asp:Label ID="Label1" runat="server" /> 


<div style="font-size: x-small; width: 100%; clear: both">     

<p><b><asp:Label ID="Label2" runat="server" /></b></p>     

<asp:DropDownList ID="DropDownList1" runat="server" /> 


DropDownList Control Code

if (!IsPostBack)
            Sitecore.Data.Items.Item item = Sitecore.Context.Item;
            if (item == null) return;
            Sitecore.Data.Items.Item home = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
            if (home == null) return;

            const string query = ".//*[@@templatename='TIC-StrategicPage']";
            Sitecore.Data.Items.Item[] children = home.Axes.SelectItems(query);
            if (children == null) return;

            Label1.Text = "C# User Control Real-time Output";
            string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
            //using &#58; for colon and &#160; for space
            Label2.Text = ("Item&#58;&#160;" + displayName);

            DropDownList1.DataSource = children;
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "id";