ExtendedDropDownList Demo

ExtendedDropDownList control extends the functionality of the standard ASP NET DropDownList Control, providing support for the select's OPTGROUP tag, which the DropDownList control doesn't know about. It also keeps the attributes of the ListItem class in the viewstate, functionality that lacks in the DropDownList control. ExtendedDropDownList control has also strong-type properties for the class name of the ListItem (CssClass and GroupCssClass properties).





ExtendedDropDownList populated from code-behind:


ExtendedDropDownList populated from ASPX:


			<%@ Page Language="C#" MasterPageFile="~/Demo.master" AutoEventWireup="true" 
    Inherits="SharpPieces.DemoApp.ExtendedDropDownListFunctionality" 
    CodeBehind="extendeddropdownlistfunctionality.aspx.cs" %>

<asp:Content ID="Content2" runat="Server" ContentPlaceHolderID="headPlaceHolder">
    <title>SharpPieces - ExtendedDropDownList Demo - Live Demo</title>
</asp:Content>
<asp:Content ID="Content3" runat="Server" ContentPlaceHolderID="descriptionPlaceholder">
    <h1>ExtendedDropDownList Demo</h1>
        
    <p>ExtendedDropDownList control 
    extends the functionality of the standard ASP NET DropDownList Control, providing support for the 
    select's OPTGROUP tag, which the DropDownList control doesn't know about. It also keeps the 
    attributes of the ListItem class in the viewstate, functionality that lacks in the DropDownList 
    control. ExtendedDropDownList control has also strong-type properties for the class name of the 
    ListItem (CssClass and GroupCssClass properties).</p>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="demoPlaceholder" Runat="Server">
    <asp:Button ID="btnPostback" Text="Do postback!" runat="server" /><br />
        ExtendedDropDownList populated from code-behind: <br />
        <piece:ExtendedDropDownList ID="ddl" runat="server">
        </piece:ExtendedDropDownList><br />
        <hr />
        ExtendedDropDownList populated from ASPX: <br />
        <piece:ExtendedDropDownList ID="ExtendedDropDownList1" runat="server">
            <ExtendedItems>
                <piece:ExtendedListItem GroupingText="Group 1" Text="My item" 
                Value="1" GroupingType="New" GroupID="myID"></piece:ExtendedListItem>
                <piece:ExtendedListItem Text="My item2" Value="2" 
                GroupingType="inherit"></piece:ExtendedListItem>
            </ExtendedItems>
        </piece:ExtendedDropDownList><br /><br />
       
       <asp:Label ID="lblSelGroup" runat="server"></asp:Label>
       
</asp:Content>
		
			using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SharpPieces.Web.Controls;
using System.Text;

namespace SharpPieces.DemoApp
{

    /// <summary>
    /// The Extended DropDownList Functionality page.
    /// </summary>
    public partial class ExtendedDropDownListFunctionality : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //populate combobox

                this.ddl.ExtendedItems.Add("Choose an item...");
                this.ddl.ExtendedItems.Add(new ExtendedListItem("Monitor", "12", true, 
                    ListItemGroupingType.New, "Computers"));

                this.ddl.ExtendedItems.Add(new ExtendedListItem("Mouse", "13", 
                    ListItemGroupingType.Inherit));
                this.ddl.ExtendedItems.Add(new ExtendedListItem("Keyboard", "14", 
                    ListItemGroupingType.Inherit));

                this.ddl.ExtendedItems.Add(new ExtendedListItem("iPhone", "21", true, 
                    ListItemGroupingType.New, "Phones"));
                this.ddl.ExtendedItems.Add(new ExtendedListItem("gPhone", "22", 
                    ListItemGroupingType.Inherit));
                this.ddl.ExtendedItems.Add(new ExtendedListItem("HTC S730", "23", 
                    ListItemGroupingType.Inherit));

                this.ddl.ExtendedItems[1].Attributes.Add("key1", "val1");
                this.ddl.ExtendedItems[2].Attributes.Add("key2", "val2");

                this.ddl.Items.FindByValue("23").Selected = true;
            }

            this.btnPostback.Click += new EventHandler(btnPostback_Click);
        }

        void btnPostback_Click(object sender, EventArgs e)
        {
            StringBuilder sbGroups = new StringBuilder();
            sbGroups.AppendFormat("selected item group for {0}: {1}<br />", 
                ddl.ID, ddl.SelectedGroup);
            sbGroups.AppendFormat("selected item group for {0}: {1}<br />", 
                ExtendedDropDownList1.ID,
                ExtendedDropDownList1.SelectedGroup);

            this.lblSelGroup.Text = sbGroups.ToString();
        }

    }

}