绑定DROPDOWNLIST与OPTGROUP从SQL数据源 [英] Bind Dropdownlist with optGroup from sql datasource

查看:239
本文介绍了绑定DROPDOWNLIST与OPTGROUP从SQL数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绑定应该按区域分组六国的一个DropDownList,我发现了一个样本code从下面的链接,

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

<一个href=\"http://www.$c$cproject.com/KB/custom-controls/DropDownListOptionGroup.aspx?msg=3984074#xx3984074xx\" rel=\"nofollow\">http://www.$c$cproject.com/KB/custom-controls/DropDownListOptionGroup.aspx?msg=3984074#xx3984074xx

我想要的国家列表与此相同。但问题是,我想从SQL结果DropDownList的绑定。我曾尝试以下,但没有工作,

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.

推荐答案

您可以编写一个自定义服务器控件和使用含有分隔文本和区域中的数据源女巫的 | ,然后把它分解时使用

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

        }
    }
}

这篇关于绑定DROPDOWNLIST与OPTGROUP从SQL数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆