Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

DropDown List With OptGroup Support

3.33/5 (6 votes)
18 Aug 2013CPOL 35.5K   1.8K  
How to add OptGroup support to your dropdown list control

Introduction

This tip shows you how to add OptGroup support to your dropdown list control.

Using the Code

  1. Download the file in the attachment & add the two folders App_Browser & App_Code to your project.
  2. After that, use the following code to create & populate your dropdown List control.
    C#
    //To bind data to dropDown Use ListItem
    
    ListItem item1 = new ListItem("A", "1");
    item1.Attributes["OptionGroup"] = "Alphabet";
     
    ListItem item2 = new ListItem("B", "2");
    item2.Attributes["OptionGroup"] = "Alphabet";
     
    ListItem item3 = new ListItem("One", "3");
    item3.Attributes["OptionGroup"] = "Numbers";
     
    ListItem item4 = new ListItem("Two", "4");
    item4.Attributes["OptionGroup"] = "Numbers";
         
    ddlItems.Items.Add(item1);
    ddlItems.Items.Add(item2);
    ddlItems.Items.Add(item3);
    ddlItems.Items.Add(item4);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)