Sha*_*nna 3 css c# asp.net gridview onmouseover
我有一个GridView,我想在MouseOver行时更改单元格颜色.我尝试了以下方法:
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#c8e4b6'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
e.Row.Cells[1].Attributes.Add("onmouseover", "this.style.backgroundColor='green'");
e.Row.Cells[1].Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
Run Code Online (Sandbox Code Playgroud)
行颜色完美变化.但是只有当鼠标在该单元格上移动时,单元格颜色才会改变.
有没有办法在鼠标在行上时更改单元格颜色?
使用它会改变你的细胞颜色
if (e.Row.RowType = DataControlRowType.DataRow)
{
string onmouseoverStyle = "this.style.backgroundColor='blue'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
e.Row.Cells[1].Attributes.Add("onmouseover",onmouseoverStyle);
e.Row.Cells[1].Attributes.Add("onmouseout",onmouseoutStyle);
}
Run Code Online (Sandbox Code Playgroud)
你可以根据你自己的行修改它
你也可以使用这段代码
if (e.Row.RowType == DataControlRowType.DataRow)
{
string onmouseoverStyle = "this.style.backgroundColor='blue'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
string onmouseoverStyle1 = "this.style.backgroundColor='green'";
string onmouseoutStyle1 = "this.style.backgroundColor='white'";
e.Row.Attributes.Add("onmouseover", onmouseoverStyle1);
e.Row.Attributes.Add("onmouseout", onmouseoutStyle1);
e.Row.Cells[1].Attributes.Add("onmouseover", onmouseoverStyle);
e.Row.Cells[1].Attributes.Add("onmouseout", onmouseoutStyle);
}
Run Code Online (Sandbox Code Playgroud)
我认为你必须Cells[1]在鼠标上设置事件处理程序的样式.
您不应该设置单元格的onmouseover和onmouseout属性,因为只有在鼠标悬停在它上面时才会起作用,而不是在整行上.
下面的代码将描述更多:
我有GridView名称GridView1,我有Javascript函数来处理鼠标悬停和鼠标移出事件如下
<script type="text/javascript" >
function onMouseOver(rowIndex) {
var gv = document.getElementById("GridView1");
var rowElement = gv.rows[rowIndex];
rowElement.style.backgroundColor = "#c8e4b6";
rowElement.cells[1].style.backgroundColor = "green";
}
function onMouseOut(rowIndex) {
var gv = document.getElementById("GridView1");
var rowElement = gv.rows[rowIndex];
rowElement.style.backgroundColor = "#fff";
rowElement.cells[1].style.backgroundColor = "#fff";
}
</script>
Run Code Online (Sandbox Code Playgroud)
在RowDataBound事件处理程序中,尝试将onmouseover和onmouseout属性添加到所有行,由Javascript函数,onMouseOver和onMouseOut处理
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "onMouseOver('" + (e.Row.RowIndex + 1) + "')";
e.Row.Attributes["onmouseout"] = "onMouseOut('" + (e.Row.RowIndex + 1) + "')";
}
}
Run Code Online (Sandbox Code Playgroud)
GridView标记应该是这样的:
<asp:GridView ID="GridView1" runat="server" ... OnRowDataBound="GridView1_RowDataBound">
Run Code Online (Sandbox Code Playgroud)
我希望这将有所帮助.