我有一个gridview与alternatingRowStyle属性设置.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged" AlternatingRowStyle-BackColor="#f0f1f3">
Run Code Online (Sandbox Code Playgroud)
我还想在光标移动时突出显示行:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当鼠标移过该行时,它将恢复为白色,而不是之前的颜色,这在一半的行中是不同的.我想我可以在为每个"onmouseove"事件替换它之前保存当前的rowcolor,但是如果快速鼠标移动可能会搞砸了,这似乎很昂贵且令人担忧.
我没有看到gridview行的属性告诉我它是否是备用行但是rowindex上的简单奇数/偶数确定最好在这里?
有更好的建议吗?
谢谢.
-担
存储原始样式.然后将样式backgroundColor设置为this.originalstyle.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
e.Row.Attributes.Add("style", "cursor:pointer;");
Run Code Online (Sandbox Code Playgroud)