使用C#动态更改asp.net中列表框中每个项目的文本颜色 [英] dynamically change the textcolor of each item in listbox in asp.net with c#

查看:71
本文介绍了使用C#动态更改asp.net中列表框中每个项目的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c#

推荐答案

如果在aspx中具有静态ListItem,则动态更改asp.net中列表框中每个项目的文本颜色:
If you have static ListItems in aspx:
<asp:listitem style="color:green;">2</asp:listitem>


Visual Studio会抱怨样式"不允许属性,但可以很好地呈现.
如果您具有数据绑定的ListBox,则可以在"ondatabound"事件中执行此操作:


Visual Studio will complain about ''style'' is not allowed attribute but it will render fine.
If you have databound ListBox you can do this in ''ondatabound'' event:

protected void ListBox1_DataBound(object sender, EventArgs e)
{
	ListBox lb = sender as ListBox;
	foreach (ListItem item in lb.Items)
	{
		item.Attributes["style"] = "color:yellow;";//  or whatever color you want
	}
}


这篇关于使用C#动态更改asp.net中列表框中每个项目的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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