Onmouseover,Onmouseout属性在gridview中不起作用

时间:2013-12-27 06:54:57

标签: c# asp.net

我已经尝试了所有可能的方法来突出gridview中的一行,我无法实现这一点。我尝试了直接的CSS-Classes,javascript。请注意我犯了错误的地方。提前谢谢......

protected void test_databound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.background='#D1DDF1';";
        e.Row.Attributes["onmouseout"] = "this.style.background='#EFF3FB';";

        //e.Row.Attributes["onmouseover"] = "javascript:SetMouseOver(this)"; 
        //e.Row.Attributes["onmouseout"] = "javascript:SetMouseOut(this)";

        //e.Row.Attributes.Add("onmouseover", "className='highlighted1'");
        //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
        //e.Row.Attributes.Add("style", "cursor:pointer;");

        //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#000000'");
        //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#000000'");
        //e.Row.Attributes.Add("style", "cursor:pointer;");


        //e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#000000'");
        //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
        //e.Row.Attributes.Add("style", "cursor:pointer;");
    }


    //e.Row.Attributes.Add("onmousedown", "IsMouseDown(this,true)");
    //e.Row.Attributes.Add("onmouseup", "IsMouseDown(this,true)");
    //e.Row.Attributes.Add("onmouseover", "HighlightRow(this,true)");
    //foreach (GridViewRow grow in gvCatalogList.Rows)
    //{
    //    e.Row.Attributes.Add("onmousedown", "IsMouseDown(this)");
    //    e.Row.Attributes.Add("onmouseup", "IsMouseDown(this)");


    //    grow.Attributes["onmouseover"] = "highlight(this, true);";
    //    grow.Attributes["onmouseout"] = "highlight(this, false);";
    //    HttpResponse myHttpResponse = Response;
    //    HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);
    //    grow.Attributes.AddAttributes(myHtmlTextWriter);
    //}
}

这是我的设计页面

<asp:GridView ID="gvCatalogList" GridLines="None" runat="server" AutoGenerateColumns="False"
PageSize="5" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging_gvCatalogList" 
OnRowDataBound="test_databound">
<FooterStyle CssClass="pagination-flickr" />
<PagerStyle CssClass="pagination-flickr" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Estimation ID">
<HeaderStyle Wrap="true" Width="10%" />
<ItemTemplate>
<div style="width: 40px; white-space: normal">
<asp:Label ID="label1" Text='<%# Eval("ProjectEstimationVersionID") %>' runat="server"></asp:Label>
<asp:HiddenField ID="hiddenEstimationID" runat="server" Value='<%# Eval("ProjectEstimationID") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>

2 个答案:

答案 0 :(得分:1)

试试这个,

我仔细阅读了该帖子。从你的代码和描述中,我想你想为你的GridView的悬停行添加一个高亮效果,对吗?

如果是这样,我建议您使用以下代码来实现:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");


         e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
      }
}

它适用于我的机器,我希望这对你有帮助。

如果我误解了您的问题,请随时告诉我。

答案 1 :(得分:0)

使用cursor ='pointer'检查now..replace cursor ='hand'

protected void test_databound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.background='#D1DDF1';";
        e.Row.Attributes["onmouseout"] = "this.style.background='#EFF3FB';";
    }
}