如何将数据属性添加到用C#一个下拉菜单 [英] How to add a data-attribute to a dropdown menu with C#

查看:143
本文介绍了如何将数据属性添加到用C#一个下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的下拉列表,然后我能数据绑定到列表中。

I have a standard dropdown list and am able to databind to the list.

<asp:DropDownList runat="server" ID="ddlMake" ClientIDMode="Static" DataTextField="Name" DataValueField="URL" AppendDataBoundItems="true">
    <asp:ListItem>Select Make</asp:ListItem>
</asp:DropDownList>

我想一个数据属性添加到选项如下图所示:

I would like to add a data-attribute to the option like below:

<asp:ListItem data-siteid="<%# DataBinder.Eval(Container.DataItem, "SiteID") %>">Select Make</asp:ListItem>

我显然得到一个错误,因为它不能识别数据SITEID。

I'm obviously getting an error because it doesn't recognize the data-siteid.

这份名单是数据绑定。

任何提示将方便

推荐答案

您可以在$ C $做到这一点的C-后面。我不知道这是最优雅的方式,但它应该工作。

You could do this in the code-behind. I'm not sure if this is the most elegant approach, but it should work.

Dim dataSrc() As String = {"ABC", "123", "!@*#"}
drp.DataSource = dataSrc
drp.DataBind()
For i = 0 To drp.Items.Count - 1
    drp.Items(i).Attributes.Add("data-siteId", dataSrc(i))
Next

此外,如果这只是一些东西,是不是数据绑定,您可以考虑使用HtmlSelect控件应该工作以及

Also, if this is just something which is not databound, you could consider using the HtmlSelect control which should work as well:

<select id="drp2" runat="server">
  <option data-siteId="2">ABC</option>
  <option data-siteId="3">123</option>
  <option data-siteId="4">@*!&</option>
</select>

这篇关于如何将数据属性添加到用C#一个下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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