类库文件加: Public Sub DataGrid_MouseOn(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) '定义鼠标 If (e.Item.ItemIndex > -1) Then e.Item.Attributes.Add("onmouseover", "this.setAttribute('BKC',this.style.backgroundColor); this.style.cursor='hand';this.style.backgroundColor='#dddddd'") e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=this.getAttribute('BKC');") End If End Sub 页面调用: Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound DataGrid_MouseOn(e) end sub
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ff0000'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor;");
}
}
/// 指定DataGrid,使鼠标经过某一行时改变其颜色。
/// </summary>
/// <param name="atheDG">指定的DataGrid</param>
/// <param name="astrColor">鼠标离开时的颜色</param>
/// <param name="astrHoverColor">鼠标经过时的颜色</param>
public static void DataGridHoverScript(System.Web.UI.WebControls.DataGrid atheDG, string astrColor, string astrHoverColor)
{
//鼠标移上时变颜色
for (int i = 0; i < atheDG.Items.Count; i++)
{
if (atheDG.Items[i].ItemType.ToString() == "Item" || atheDG.Items[i].ItemType.ToString() == "AlternatingItem")
{
System.Web.UI.WebControls.TableRow theTR = (System.Web.UI.WebControls.TableRow)atheDG.Items[i].Cells[0].Parent;
theTR.Attributes.Add("onmouseover", "this.bgColor='" + astrHoverColor + "'");
theTR.Attributes.Add("onmouseout", "this.bgColor='" + astrColor + "'");
}
}
}这是我写的,你参考一下吧
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//添加自定义属性,当鼠标移过来时设置该行的背景色为"#404040",并保存原背景色
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#404040'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");}
{
string str;
int i;
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.EditItem || e.Item.ItemType==ListItemType.SelectedItem || e.Item.ItemType==ListItemType.AlternatingItem)
{
//改变鼠标所到行的背景色
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='E9ECF2'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor"); i=(int)DataBinder.Eval(e.Item.DataItem,"TID");
LinkButton btn = (LinkButton)(e.Item.FindControl("delbtn"));
if (btn!=null)
{
btn.CommandArgument = i.ToString();
}
btn.Attributes.Add("onclick", "javascript:{if(confirm('确定删除吗?')==false) return false;}");
LinkButton btn2 = (LinkButton)(e.Item.FindControl("updatebtn"));
if(btn2!=null)
{
btn2.CommandArgument=i.ToString();
}
//绑定超连接列
str=(string)DataBinder.Eval(e.Item.DataItem,"text1");
LinkButton btn1 = (LinkButton)(e.Item.FindControl("btn"));
if (btn1!=null)
{
btn1.Text=str;
}
}
}
Public Sub DataGrid_MouseOn(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
'定义鼠标
If (e.Item.ItemIndex > -1) Then
e.Item.Attributes.Add("onmouseover", "this.setAttribute('BKC',this.style.backgroundColor); this.style.cursor='hand';this.style.backgroundColor='#dddddd'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=this.getAttribute('BKC');")
End If
End Sub
页面调用:
Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
DataGrid_MouseOn(e)
end sub
很简单
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#bcfed0';");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='White';");
}
}