代码改变世界

如何把 DropDownList 某一个 Item 的 Text 改成粗体 ?

2007-07-06 23:43  晓风残月  阅读(1024)  评论(0编辑  收藏  举报

这是昨天 CSDN 里面的一个帖子 

http://community.csdn.net/Expert/TopicView3.asp?id=5637788

今天发现竟然成为头条了。

本以为,可以像设置字体颜色一样设置字形,结果大出意料,只有 FF 才可以呈现出预期的效果。

测试代码:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected 
void Page_Load(object sender, EventArgs e)
    
{
        ListItem item 
= null;
        item 
= new ListItem("Red--Setting by code-programming""red");        
        item.Attributes[
"style"= "color: " + item.Value;
        DropDownList1.Items.Add(item);

        item 
= new ListItem("Bold Font?--Setting by code-programming""bold");
        item.Attributes[
"style"= "font-weight:" + item.Value;
        DropDownList1.Items.Add(item);
        
        item 
= new ListItem("none");
        DropDownList1.Items.Add(item);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>ListItemStyle_FontWeight_Test</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:DropDownList ID="DropDownList1" runat="server">        
        
<asp:ListItem style="color:green">Green--Setting by declarative-style</asp:ListItem>
        
<asp:ListItem style="font-weight:bold">Blod Font?----Setting by declarative-style</asp:ListItem>
        
</asp:DropDownList></div>
    
</form>
</body>
</html>

效果:




只有 FF 最感冒了, 但是当选中某一项后,颜色恢复到整个 select 的设置。