asp.net:列表框中的项目改变颜色 [英] asp.net: change color of listbox items
问题描述
这是一个WebForm列表框被填充由一个数据源的SQL Server 2008。
a listbox on a webform is being populated by a datasource on a sql server 2008.
根据在列表框中的文本,我会liek该特定项目的backgroudn颜色是一种特定的颜色
depending on the text in the list box i would liek the backgroudn color of that specific item to be a specific color
例如,如果这些是在列表中的项目:
for example if these are the items in the list:
AA item 1
AA item 2
BB item 3
BB item 4
AA item 5
如果该项目始于 AA
,然后使背景绿色
如果该项目的生命与 BB
然后使它蓝色
if the item begins with AA
, then make the background green
and if the item beings with BB
then make it blue
我怎样才能做到这一点?
how can i do this?
该解决方案可以是客户端或服务器端,不事关我
the solution can be client or server side, doesnt matter to me
我正在currenlty这样做:
i am currenlty doing this:
function colorproblemlist() {
ob = document.getElementById('lstProblems');
for (var i = 0; i < ob.options.length; i++) {
if (ob.options[i].value.indexOf('AA')!=-1) {
ob.options[i].style.color = "red";
}
}
}
和它的工作伟大的!
我有以下并发症。
第一列正如你看到如下:
the first column as you see below:
AA item 1
AA item 2
BB item 3
BB item 4
AA item 5
将不可见
只有第二个是可见的:
Item 1
Item 2
...
此列:
AA
AA
..
在从该数据被拉到数据库表中的字段,我需要的颜色是基于该字段。
is a field in the database table from which this data is pulled and i need the color to be based on that field.
我怎样才能做到这一点?>
how can i do this?>
推荐答案
是这样的:
function colorproblemlist() {
ob = document.getElementById('lstProblems');
for (var i = 0; i < ob.options.length; i++) {
var option = ob.options[i];
switch(option.value.substr(0,2))
{
case "AA":
option.style.color = "Red";
break;
case "BB":
option.style.color = "Green";
break;
}
option.value = option.value.slice(3); //Assumption of 'AA '
}
}
基于该除去的AA,从HTML BB标志,修改客户端上的颜色将不再可能。的
这篇关于asp.net:列表框中的项目改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!