简体   繁体   English

如何在 webforms 中的选择中用自定义 HTML 替换 ListItem

[英]How to replace ListItem with custom HTML in select in webforms

I'm using WebForms in an application where my code behind generates an HtmlSelect:我在一个应用程序中使用 WebForms,其中我的代码生成一个 HtmlSelect:

HtmlSelect teamfilter = new HtmlSelect();
teamfilter.ID = "team_filter_" + studioId;
teamfilter.Attributes.Add("class", "col-md-9");
teamfilter.Attributes.Add("multiple", "true");

ListItem listItem = new ListItem("All " + studioName, "studio_" + studioId);
listItem.Attributes.Add("data-type", "studio");
teamfilter.Items.Add(listItem);

On the client side I'm using a plugin which allows ListItems to be grouped with a header in the list using a tag.在客户端,我使用了一个插件,该插件允许使用标签将 ListItems 与列表中的标题分组。 In other words the rendered HTML must appear as:换句话说,呈现的 HTML 必须显示为:

<select id="blah">
    <optgroup label="Games">
        <option value="1">One</option>
        <option value="2">Two</option>
    </optgroup>
</select>

I need to generate this Select in the code behind using the HtmlSelect class and the ListItem class but how do I add an item so that it renders the starting and closing optgroup tags?我需要使用 HtmlSelect 类和 ListItem 类在后面的代码中生成这个 Select 但是我如何添加一个项目以便它呈现开始和结束 optgroup 标签?

I think you can't do it only in code behind.我认为你不能只在后面的代码中做到这一点。

Keep the code behind generating the HtmlSelect this way you're doing, and with jQuery, maybe you can do something like this:将代码保留在以您正在执行的方式生成HtmlSelect ,并且使用 jQuery,也许您可​​以执行以下操作:

$("select[id='blah'] option").wrap("<optgroup label='Games'></<optgroup>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM