使用 jquery 选择将代码隐藏中的组填充到 ListBox

populate ListBox with groups in code-behind to ListBox using jquery chosen

我正在使用 Web 表单 (ASP.NET / C#) 编写 Web 应用程序。我有一个使用 jquery 插件的列表框控件,已选择。我使用数据库调用填充代码隐藏中的列表框。它工作正常,所以我想将组添加到列表框。数据是列表显示的,不是我设置的组。
我相信问题出在所选择的查询插件上。我可能需要以某种方式设置此插件的选项,但我还没有看到任何关于如何操作的文档。 这是我的 javascript / HTML 代码:

<script type="text/javascript">
     $(document).ready(function () {
         $(".chosen-select").chosen({
             search_contains: true,
             no_results_text: "Sorry, no match!",
             allow_single_deselect: true
         });
         $('.chosen-container').css('width', '600px');
     });
</script>
 <asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
                    data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
        </asp:ListBox>

这是我用来填充列表框的 C# 代码:

foreach (DataRow row in m_dtRecipients.Rows)
{
     ListItem recItem = new ListItem(row["Name"].ToString(), row["ID"].ToString());
     if (row["UserID"].ToString().Equals("Global"))
     {
         recItem.Attributes[OPT_GROUP_ATTRIBUTE] = GLOBAL_GROUP;
     }
     else if (row["UserID"].ToString().Equals(m_strUserID))
     {
         recItem.Attributes[OPT_GROUP_ATTRIBUTE] = PERSONAL_GROUP;
     }
     else
     {
         recItem.Attributes[OPT_GROUP_ATTRIBUTE] = INDIVIDUAL_GROUP;
     }
     lstBoxTo.Items.Add(recItem);
}

数据正确,列表框显示数据但不分组。 我如何获得选择的 jquery 插件来分组显示数据?

谢谢。

更新

我了解到ListBox 和DropDownList 不支持optgroup。所以我想尝试这个解决方案,但无法理解 javascript。 在后面的代码中,属性被添加到每个ListItem:

foreach (ListItem item in ((DropDownList)sender).Items)
        {
            if (System.Int32.Parse(item.Value) < 5)
                item.Attributes.Add("classification", "LessThanFive");
            else
                item.Attributes.Add("classification", "GreaterThanFive");

        } 

这是javascript

<script>
    $(document).ready(function() {
        //Create groups for dropdown list
        $("select.listsmall option[@classification='LessThanFive']").wrapAll("<optgroup label='Less than five'>");
        $("select.listsmall option[@classification='GreaterThanFive']").wrapAll("<optgroup label='Greater than five'>"); 
    });

我不明白“select.listsmall”代表什么地方。我尝试使用我的 ListBox ID,但出现异常。 谁能解释一下javascript的这一部分? 谢谢。

更新 这就是我使用上面的代码隐藏和 javascript 的方式:

private const string OPT_GROUP_ATTRIBUTE = "grouping";
private const string GLOBAL_GROUP = "Global Groups";
private const string PERSONAL_GROUP = "Personal Groups";
private const string INDIVIDUAL_GROUP = "Individuals";

    foreach (DataRow row in m_dtRecipients.Rows)
    {
     ListItem recItem = new ListItem(row["Name"].ToString(), row["ID"].ToString());
     if (row["UserID"].ToString().Equals("Global"))
     {
        recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, GLOBAL_GROUP);                          
     }
     else if (row["UserID"].ToString().Equals(m_strUserID))
     {
        recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, PERSONAL_GROUP);                     
     }
     else
     {
        recItem.Attributes.Add(OPT_GROUP_ATTRIBUTE, INDIVIDUAL_GROUP);
     }
     lstBoxTo.Items.Add(recItem);
    }

这是列表框HTML:

<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple" 
  data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>

这是javascript:

  $(document).ready(function () {
             $(".chosen-select").chosen({
                 search_contains: true,
                 no_results_text: "Sorry, no match!",
                 allow_single_deselect: true,
                 group: true
             });
             $('.chosen-container').css('width', '600px');

             //Create groups for dropdown list
             $("select.chosen-select option[@grouping='Global Groups']").wrapAll("<optgroup label='Global Groups'>");
             $("select.chosen-select option[@grouping='Personal Groups']").wrapAll("<optgroup label='Personal Groups'>");
             $("select.chosen-select option[@grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
         });

我有什么遗漏或错误的地方吗?

更新 好吧,如果我从 attribute=value 中删除“@”,它不会抛出异常,但它也不会对列表进行分组。

我想通了我的问题...选择-jquery 配置函数必须在 'wrapAll' 函数之后。这是变化:

$(document).ready(function () {       
        //Create groups for dropdown list
        $(".chosen-select option[grouping='GlobalGroups']").wrapAll("<optgroup label='Global Groups'>");
        $(".chosen-select option[grouping='PersonalGroups']").wrapAll("<optgroup label='Personal Groups'>");
        $(".chosen-select option[grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");

        //Configure the ListBox using the 'chosen' jquery plugin
        $(".chosen-select").chosen({
            search_contains: true,
            no_results_text: "Sorry, no match!",
            allow_single_deselect: true
        });
        $('.chosen-container').css('width', '600px');
    });