碰到一个需求,在下拉框里面有一项需要用红色表示,显得醒目一些。
一开始就想当然地写 dropdownlist1.attribute.add("style","color:red")
怎么试来试去都不行,然后用htmlselect来做却可以。
我想是不是dropdownlist1要用其他的方法来实现。第二天在csdn上查找
dropdownlist,颜色,发现其中有一篇提到,其中有两个连接
http://www.c-sharpcorner.com/Code/2003/July/DropDownListBox.asp
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338
才知道dropdownlist有一个bug,不支持Attribute,只能用htmlselect代替。
修改了一下自己的代码,例子代码如下:
Dim ht As New Hashtable
dim i as integer
ht.Add("1", "red")
ht.Add("2", "green")
With dropdownlist1
.DataValueField = "key"
.DataTextField = "value"
.DataSource = ht
.DataBind()
For i = 0 To .Items.Count - 1
.Items(i).Attributes.Add("style", "color:" + .Items(i).Text)
Next
End With