将 Dropdownlist 与来自 sql 数据源的 optGroup 绑定

发布于 2024-11-28 11:43:05 字数 506 浏览 4 评论 0原文

我必须将下拉列表与应按区域分组的国家绑定,我从以下链接找到了示例代码,

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

我希望国家/地区列表与此相同。但问题是我想从 sql 结果绑定下拉列表。我尝试过以下方法,但没有成功,

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

任何人都知道解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-12-05 11:43:05

您可以编写自定义服务器控件并使用包含由 | 分隔的文本和区域的数据源,然后在使用时将其拆分。

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

        }
    }
}

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

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文