注册 登录
编程论坛 ASP.NET技术论坛

[求助]gridview随光标的移动改变颜色

笑看人生活快乐 发布于 2007-05-21 08:24, 766 次点击

我们在用鼠标在gridview上停留时,gridview就会改变颜色,如下所示:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");


}
}

}

怎么样将其修改成为,当光标在上面停留的时候改变颜色.
请高手赐教!
8 回复
#2
rainic2007-05-21 09:14
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RowBackColor = RowBackColor == temp_bgcolor ? Tools.GetColorString(((GridView)sender).AlternatingRowStyle.BackColor) : temp_bgcolor;
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='" + color_hover + "'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + RowBackColor + "'");
//为"删除"加上"确认是否删除"
LinkButton btn_delete = (LinkButton)e.Row.FindControl("btnDel");
if (btn_delete != null)
btn_delete.Attributes.Add("onclick", "return confirm('确认要删除吗?');");
}
}

//说明: Tools.GetColorString是我自己写的获取颜色字符串的方法,代码在http://www.rainic.com/article_view.asp?type=2&id=756
//后面三行用于加删除提示,你不用就把它们去掉...如果还有不明白可以问

[此贴子已经被作者于2007-5-21 9:18:38编辑过]

#3
tel19822007-05-21 09:21
谢谢rainic,这正是我需要的。
#4
tel19822007-05-21 09:49
我会用DataGrid的变色
private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType!=ListItemType.Header)
 {
  e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
  e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=\""+ "#EFF3F7"+"\"");
 }
}
但是你说的这个我还没有实现过。
如果上面斑竹的说得不行
你就参照着这个改一下吧。
#5
rainic2007-05-21 10:00
e.Item.Style["BACKGROUND-COLOR"]

这个比我的方便点
#6
foshan2007-05-21 10:09

有个问题请教:在此基础上,如何实现对于 已选定 的行,光标的移过 已选定的行 不改变其颜色,只改变非已选定行的颜色?谢谢!

[此贴子已经被作者于2007-5-21 10:12:15编辑过]

#7
rainic2007-05-21 10:19
以下是引用foshan在2007-5-21 10:09:41的发言:

有个问题请教:如何实现对于 已选定 的行,光标的移过 已选定的行 不改变其颜色?谢谢!

这个就比较难了,我没有做ASP.NET方面的代码..只可以提示你一下...

每一行都要用一个变量(或行的属性,或数组的其中一项,反正就是一个值)记录它是否被选中..当然这是客户端JS中的变量

你看一下下面那个网页的下面,我是用td的Title来记录它是否被选中,你可以参考一下
http://www.d.now.cn/order/dish.php?id=102

#8
tel19822007-05-21 11:42

你说的这个我好像在哪里见过,等等,我给你看一下。

#9
foshan2007-08-08 11:19

旧事重提:在此基础上,如何实现对于 已选定 的行,光标的移过 已选定的行 不改变该行的背景颜色,只改变非已选定行的颜色?谢谢!

1