C# 如何更改行鼠标悬停时GridView单元格的颜色
我有一个GridView,我想在鼠标移到行上时更改单元格颜色。我尝试了以下方法:C# 如何更改行鼠标悬停时GridView单元格的颜色,c#,asp.net,css,gridview,onmouseover,C#,Asp.net,Css,Gridview,Onmouseover,我有一个GridView,我想在鼠标移到行上时更改单元格颜色。我尝试了以下方法: 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.b
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'");
行的颜色变化非常完美。但是,只有当鼠标在单元格上移动时,单元格颜色才会改变
当鼠标位于行时,有没有办法改变单元格颜色?试试这个
e.Row.Attributes.Add("onmouseover","this.originalcolor=this.style.backgroundColor;" + " this.style.backgroundColor='#FDCB0A';");
功能设置高度(txtdesc){
txtdesc.style.backgroundColor='蓝色';
}
功能输出(txtdesc){
txtdesc.style.backgroundColor='绿色';
}
使用此选项,它将更改单元格颜色
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);
}
您也可以根据您自己对行进行修改
您也可以使用此代码
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);
}
我认为您必须在鼠标悬停事件处理程序中设置
单元格[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>
GridView标记应如下所示:
<asp:GridView ID="GridView1" runat="server" ... OnRowDataBound="GridView1_RowDataBound">
我希望这会有所帮助。试试这个:
<style type="text/css">
#GridView1 tr.rowHover:hover
{
background-color: Yellow;
font-family: Arial;
}
</style>
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" RowStyle-CssClass="rowHover" ClientIDMode="Static" />
#GridView1 tr.rowHover:悬停
{
背景颜色:黄色;
字体系列:Arial;
}
谢谢您的回复。行的颜色会改变。但我想更改鼠标上方特定单元格的颜色。尝试在鼠标悬停时删除特定单元格的背景色,然后在鼠标悬停时添加所需的背景色。就是这样。我不知道如何删除鼠标上的特定“单元格”或在“行”上添加颜色。请检查您是否发现此功能有用,它只更改行颜色而不更改单元格颜色。为什么您可以在JavaScript中尝试。请帮我完成此操作。谢谢。这只在我移动到壁细胞上时有效。如果我将鼠标移到行上,单元格的颜色不会改变鼠标的颜色。如果你改变颜色,则先改变行的颜色,然后再改变单元格的颜色。否则,它将只显示行的颜色
<asp:GridView ID="GridView1" runat="server" ... OnRowDataBound="GridView1_RowDataBound">
<style type="text/css">
#GridView1 tr.rowHover:hover
{
background-color: Yellow;
font-family: Arial;
}
</style>
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" RowStyle-CssClass="rowHover" ClientIDMode="Static" />