Search code examples
asp.netsqldrop-down-menudatasourceoptgroup

Bind Dropdownlist with optGroup from sql datasource


I have to bind a Dropdownlist with coutries which should be grouped by region, I have found a sample code from the following link,

http://www.codeproject.com/KB/custom-controls/DropDownListOptionGroup.aspx?msg=3984074#xx3984074xx

I wanted the country list same as this. But problem is that I want to bind the dropdownlist from sql result. I have tried the following but didn't work,

ddlCountry.DataSource = CountryDtoCollection;
ddlCountry.DataBind();
ddlCountry.Attributes.Add("OptionGroup", "Region");

any one knows any solution for this.


Solution

  • you can write a custom server control and use the data source witch containing the text and region separated by | then split it when use.

    [ToolboxData("<{0}:CustomDropDownList runat=server></{0}:CustomDropDownList>")]
    public class CustomDropDownList : DropDownList
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.Items.Count > 0)
            {
                bool selected = false;
                bool optGroupStarted = false;
                string lastOptionGroup = string.Empty;
                for (int i = 0; i < this.Items.Count; i++)
                {
                    ListItem item = this.Items[i];
                    if (item.Enabled)
                    {
                        if (lastOptionGroup != item.Text.Split("|")[1])
                        {
                            if (optGroupStarted)
                            {
                                writer.WriteEndTag("optgroup");
                            }
                            lastOptionGroup = item.Text.Split("|")[1];
                            writer.WriteBeginTag("optgroup");
                            writer.WriteAttribute("label", lastOptionGroup);
                            writer.Write('>');
                            writer.WriteLine();
                            optGroupStarted = true;
                        }
                        writer.WriteBeginTag("option");
                        if (item.Selected)
                        {
                            if (selected)
                            {
                                this.VerifyMultiSelect();
                            }
                            selected = true;
                            writer.WriteAttribute("selected", "selected");
                        }
                        writer.WriteAttribute("value", item.Value, true);
                        if (item.Attributes.Count > 0)
                        {
                            item.Attributes.Render(writer);
                        }
                        if (this.Page != null)
                        {
                            this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);
                        }
                        writer.Write('>');
                        HttpUtility.HtmlEncode(item.Text.Split("|")[0], writer);
                        writer.WriteEndTag("option");
                        writer.WriteLine();
                    }
                }
                if (optGroupStarted)
                {
                    writer.WriteEndTag("optgroup");
                }
    
            }
        }
    }