ASP.NET中GridView實現行滑鼠滑過及選擇變色
https://blog.csdn.net/u010568463/article/details/44835833
一、滑鼠滑過變色
只要要給GridView新增OnRowDataBound方法即可。
前臺程式碼:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView_RowDataBound">
</asp:GridView>
後臺程式碼:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判斷是否為資料行(排除標題行)
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "col=this.style.backgroundColor;this.style.backgroundColor='#95B8FF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=col");
}
}
二、滑鼠單擊(雙擊)變色
同上,後臺程式碼稍作修改即可,若要雙擊變色只要將onclick改為ondblclick即可
後臺程式碼:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判斷是否為資料行(排除標題行)
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.style.backgroundColor=oldc;}oldc=this.style.backgroundColor;this.style.backgroundColor='#e6c5fc';window.oldtr=this;");
}
}
三、同時實現以上變色
後臺程式碼:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判斷是否為資料行(排除標題行)
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(window.oldtr==null||window.oldtr!=this){col=this.style.backgroundColor;this.style.backgroundColor='#95B8FF'}");
e.Row.Attributes.Add("onmouseout", "if(window.oldtr==null||window.oldtr!=this){this.style.backgroundColor=col}");
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.style.backgroundColor=oldc;}this.style.backgroundColor='#e6c5fc';window.oldtr=this;oldc=col;");
}
}
---------------------
作者:楓葉_雨
來源:CSDN
原文:https://blog.csdn.net/u010568463/article/details/44835833
版權宣告:本文為博主原創文章,轉載請附上博文連結!