在ItemDataBound事件中为控件加载属性时遇到的问题

jlqidan 2003-09-01 03:06:19
为DataGrid控件应用了如下属性<AlternatingItemStyle BackColor="white"></AlternatingItemStyle>
在ItemDataBound事件处理函数中有如下语句:
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='LightGrey';this.style.cursor='hand'");

现在的问题是当我鼠标移开时,DataGrid控件被选中的那行颜色不能回复,试过几种写法,均达不到目的.

本来想如此这般:(也是在ItemDataBound事件处理函数中,但在上面那条语句之前)
Color cl = new Color();
cl = e.Item.BackColor;
可是这样也取不到当前行的颜色.跟踪执行发现cl为empty.
谁有更好的办法吗?
...全文
50 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞不动 2003-09-01
  • 打赏
  • 举报
回复
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//创建项时发生
if(e.Item.ItemType!=ListItemType.Header&& e.Item.ItemType!=ListItemType.Pager)
{

if(e.Item.ItemType!=ListItemType.AlternatingItem)
{
e.Item.Attributes["onmouseout"]="javascript:this.style.backgroundColor='#E7E7FF';";
e.Item.Attributes["onmouseover"]="javascript:this.style.backgroundColor='#99cccc';";
}
else
{
e.Item.Attributes["onmouseout"]="javascript:this.style.backgroundColor='#F7F7F7';";
e.Item.Attributes["onmouseover"]="javascript:this.style.backgroundColor='#99cccc';";
}

}

}
cnhgj 2003-09-01
  • 打赏
  • 举报
回复
用样式

//html中

<style type="text/css">
.normal { }
.up { BACKGROUND-COLOR: #92d0df }
</style>

.cs中

e.item.Attributes("onMouseOver")="this.className='up'"
e.item.Attributes("onMouseOut")="this.className='normal'"
junmingl 2003-09-01
  • 打赏
  • 举报
回复
我写的一个关于变色的例子,供参考:
if (e.Item.ItemIndex != -1)
{
e.Item.Attributes.Add("onmouseover", "this.bgColor='#fafad2'");
if (e.Item.ItemIndex % 2 == 0 )
{
e.Item.Attributes.Add("bgcolor", "#E7E7FF");
e.Item.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('dgrdPfcx').singleValue");
}
else
{
e.Item.Attributes.Add("bgcolor", "#F7F7F7");
e.Item.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('dgrdPfcx').oldValue");
}
}
else
{
dgrdPfcx.Attributes.Add("oldValue", "#F7F7F7");
dgrdPfcx.Attributes.Add("singleValue", "#E7E7FF");
}

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧