Click here to Skip to main content
Click here to Skip to main content

ASP.NET DropDownList with OptionGroup support

, 12 Sep 2006
Rate this:
Please Sign up or sign in to vote.
An implementation of a DropDownList Control Adapter which provides OptionGroup support.


ASP.NET 2.0, for all its bells and whistles, lacks the odd bit of functionality for reasons completely unknown. One such notable omission is that of OptionGroup (<optgroup>) support in the DropDownList control. For those unfamiliar with the <optgroup> element, it is part of the XHTML standard, and has the effect of categorising items in a <select>, as the following image shows.

Option Groups

When I first began to look for a solution to implement this in ASP.NET, I found very few articles on the topic that offered any viable solution. Another CodeProject member came up with a nice clean solution, but it looked like a lot of code and I was convinced there was an easier way. After reading some comments, it became apparent a Control Adapter was the way to go. Control Adapters are new in ASP.NET 2.0, and allow the developer to override the rendering behaviour of any control, very powerful stuff! Furthermore, Control Adapters are used in conjunction with a browser file, so specific browsers may be targeted, if required. Armed with that knowledge, the solution became simple. The attached download contains the requisite files to implement this solution in your own projects, but for posterity, I paste it here also.

public class DropDownListAdapter : 
       System.Web.UI.WebControls.Adapters.WebControlAdapter {
    protected override void RenderContents(HtmlTextWriter writer) {
        DropDownList list = this.Control as DropDownList;
        string currentOptionGroup;
        List<string> renderedOptionGroups = new List<string>();
        foreach(ListItem item in list.Items) {
            if(item.Attributes["OptionGroup"] == null) {
                RenderListItem(item, writer);
            } else {
                currentOptionGroup = item.Attributes["OptionGroup"];
                if(renderedOptionGroups.Contains(currentOptionGroup)) {
                    RenderListItem(item, writer);
                } else {
                    if(renderedOptionGroups.Count > 0) {
                    RenderListItem(item, writer);
        if(renderedOptionGroups.Count > 0) {
    private void RenderOptionGroupBeginTag(string name, 
                 HtmlTextWriter writer) {
        writer.WriteAttribute("label", name);
    private void RenderOptionGroupEndTag(HtmlTextWriter writer) {
    private void RenderListItem(ListItem item, 
                 HtmlTextWriter writer) {
        writer.WriteAttribute("value", item.Value, true);
        if(item.Selected) {
            writer.WriteAttribute("selected", "selected", false);
        foreach(string key in item.Attributes.Keys) {
            writer.WriteAttribute(key, item.Attributes[key]);
        HttpUtility.HtmlEncode(item.Text, writer);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

NewsError while Try to run sample PinmemberDinesh Balaji24-Jun-14 4:26 
GeneralRe: Error while Try to run sample Pinmembersfjaffer16-Sep-14 4:17 
GeneralMy vote of 1 PinmemberMember 100194966-Jun-14 6:15 
SuggestionEnabling the code for listbox PinmemberMichael Demeersseman24-Nov-13 22:24 
QuestionIdeally you should include the code in a working Visual Studio Project Pinmemberliran bar niv24-Oct-13 11:14 
AnswerRe: Ideally you should include the code in a working Visual Studio Project PinprofessionalPIEBALDconsult24-Oct-13 12:14 
Bugnot working Pinmemberbhawana13198918-Jul-12 20:02 
Suggestion&lt;optgroup&gt; support with web componenets - An alternate way PinmemberShan Veerakutty12-Sep-11 10:55 
GeneralRe: <optgroup> support with web componenets - An alternate way Pinmemberfengjunkuan20063-Jul-12 0:07 
QuestionBinding data from Db Pinmembersudheshna1235-Aug-11 21:46 
GeneralCan you specific a license or terms or use... Pinmemberhhearst24-Apr-11 4:53 
GeneralRe: Can you specific a license or terms or use... Pinmemberlotuspro25-Apr-11 12:07 
GeneralDropdowlist with optiongroup support using ajax PinmemberMember 452360631-Aug-10 1:43 
GeneralListBoxAdapter Pinmembervoleona1-Feb-10 13:51 
QuestionEvent validation errors PinmemberMember 446930417-Aug-09 10:00 
AnswerRe: Event validation errors PinmemberKishore.ssit7-Dec-09 22:31 
AnswerRe: Event validation errors PinmemberSean_ST2-Sep-10 12:55 
GeneralRe: Event validation errors PinmemberMember 392364420-Oct-10 18:18 
GeneralVB Version Pinmemberianbenoliel17-Feb-09 4:14 
QuestionUse of your code in VS2008 PinmemberMember 437824011-Feb-09 3:56 
AnswerRe: Use of your code in VS2008 Pinmemberlotuspro11-Feb-09 4:20 
AnswerRe: Use of your code in VS2008 PinmemberMember 437824011-Feb-09 5:29 
Generaloptgroup style change PinmemberJay K R8-Oct-08 19:51 
AnswerRe: optgroup style change PinmemberCaptainRantflaps13-Nov-08 0:24 
Questioncombination of ungroup item [modified] Pinmemberpaidinjav17-Sep-08 0:07 
AnswerRe: combination of ungroup item PinmemberMember 195472715-Dec-08 3:11 
GeneralRe: combination of ungroup item PinmemberSameers (theAngrycodeR )29-Mar-10 2:08 
Generaloptgroup structure disappears after PostBack PinmemberMember 37370022-Sep-08 4:30 
GeneralRe: optgroup structure disappears after PostBack PinmemberLamester17-Jun-09 14:51 
GeneralRe: optgroup structure disappears after PostBack PinmemberVipul12345626-Apr-10 20:57 
GeneralRe: optgroup structure disappears after PostBack Pinmemberridgie20-Jul-10 17:52 
GeneralDrop Down list bind to sql server Pinmemberbismillah131-Aug-08 21:27 
GeneralOnSelectedIndexChanged event not working PinmemberRashuD5-Feb-08 5:30 
GeneralRe: OnSelectedIndexChanged event not working PinmemberMember 465139911-Aug-08 21:20 
GeneralRe: OnSelectedIndexChanged event not working PinmemberSivaPrakasamDotNet17-Jul-09 0:33 
GeneralRe: OnSelectedIndexChanged event not working PinmemberMember 979468530-Jan-13 0:28 
QuestionMore than one level??? Pinmemberlatiful8-Sep-07 1:33 
Generalchange of Font Style optgroup element Pinmemberaf_shamim23-Aug-07 14:01 
AnswerRe: change of Font Style optgroup element Pinmemberlotuspro24-Aug-07 0:06 
GeneralRe: change of Font Style optgroup element PinmemberNayeem Yahoo16-Jan-09 3:24 
GeneralRe: change of Font Style optgroup element PinmemberGilesHinton2-Aug-10 6:46 
GeneralUpdate to RenderContents PinmemberPedro Maia Costa20-Jul-07 2:05 
QuestionList get added again after postback Pinmemberxsoftdev19-Jul-07 6:20 
AnswerRe: List get added again after postback PinmemberPedro Maia Costa20-Jul-07 2:07 
GeneralVB version anyone... Pinmemberjaywoncoder10-May-07 13:46 
AnswerVB version with fixes below... PinmemberWolfyUK20-Jun-07 6:38 
AnswerStandalone version [modified] PinmemberPaul Appeldoorn4-Oct-07 4:53 
GeneralRe: Standalone version PinmemberMember 437800431-Jul-08 21:18 
QuestionRe: VB version with fixes below... [modified] PinmemberCaptainRantflaps13-Nov-08 0:15 
QuestionAjax [modified] Pinmemberdavidprice3520-Mar-07 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 12 Sep 2006
Article Copyright 2006 by lotuspro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid