2016-08-05 75 views
1

下拉列表需要显示具有不同文字颜色的项目。颜色由服务器端决定。使用<style>标签与CSS样式表。见例如:ASP:DropDownList Codebehind添加颜色

protected void Page_Load(object sender, EventArgs e) 
    { 
     ListItem li = CreateListItemWithColor("Hello", "myValue", "blue"); 
     employeeDropDownBox.Items.Add(li); 

    } 
public ListItem CreateListItemWithColor(string text, string value, string color) 
    { 
     //Create the list item based on input text/value 
     ListItem li = new ListItem(); 
     li.Text = text; 
     li.Value = value; 
     li.Attributes.Add("style", "color="+color); 
     return li; 
    } 

从我在其他有关格式列表项的文本,以岗位已阅读,我的一般程序似乎接近。但我的ListItem始终是黑色的。我错过了什么?


缩HTML:

<style> 
    #employeeDropDownBox { 
     height: 65px; 
     width: 425px; 
     font-size: 27px; 
     margin-left: 5px; 
    } 

</style> 

<div> 
    <asp:DropDownList ID="employeeDropDownBox" runat="server" ></asp:DropDownList> 
</div> 
+0

我没想到个别'option'元素在'select'甚至可能* *样式。这在网页开发中一直是个问题,因为不同的主机以自己的方式呈现'select'元素。通常,解决方案一直使用JavaScript插件来创建自定义元素,以模仿“select”的行为并与隐藏的“select”同步。 – David

+0

查看http://stackoverflow.com/questions/2609344/how-to-format-individual-dropdownlist-items-color-etc-during-ondatabinding-e –

+0

@MehdiDehghani额外的CSS文件是不必要的。我正在使用'