ListItem其他自定义值 [英] ListItem additional custom values

查看:84
本文介绍了ListItem其他自定义值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中使用dropdownlist,它具有表示该dropdownlist项的ListItem集合,每个ListItem仅具有两个字段来保存数据,即Value和Text字段,但这些还不够,我想为每个项目保存更多数据.让我们在其他字段中说Text1和Text2,但同时我想保留dropdownlist的DataBind()方法的相同行为.

I am using dropdownlist in asp.net, it has collection of ListItem that represents the items of the dropdownlist, each ListItem has only two fields to hold the data, Value and Text fields, but those are not enough I would like to hold more data for each item. Let's say Text1 and Text2 in additional fields, but at the same time I would like to preserve the same behavior of the DataBind() method of the dropdownlist.

推荐答案

当然,您可以使用Attributes集合:

Sure, you can use the Attributes collection:

ListItem li = new ListItem("myText", "myID");
li.Attributes.Add("data-x", "xx");
dropdown.Items.Add(li);

这将为您提供此HTML:

This will give you this HTML:

<select name="dropdown" id="dropdown">
   <option value="myID" data-x="xx">myText</option>
</select>

我建议您为自定义属性添加"data-"前缀: http://html5doctor.com/html5-custom-data-attributes/

I suggest you prefix your custom attributes with "data-": http://html5doctor.com/html5-custom-data-attributes/

这篇关于ListItem其他自定义值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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