Bind Dropdownlist with optGroup from sql datasource

走远了吗. 提交于 2019-12-12 09:03:39

问题


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.


回答1:


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");
            }

        }
    }
}


来源:https://stackoverflow.com/questions/6965514/bind-dropdownlist-with-optgroup-from-sql-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!