简体   繁体   English

使用 CSS 将鼠标悬停在 Gridview ASP.net 中

[英]mouseover hover in Gridview ASP.net using CSS

This is probably a really simple thing but I am completely new to CSS.这可能是一件非常简单的事情,但我对 CSS 完全陌生。 I just want to be able to have mouseover hover effect on my rows in gridview, changing the color of the row if it is being hovered over.我只想能够对 gridview 中的行进行鼠标悬停效果,如果悬停在行上则更改行的颜色。 I'm curious as to whether or not my CSS file is in the right place?我很好奇我的 CSS 文件是否在正确的位置? Should my Gridview.CSS be in the same folder as my gridview.aspx (I assume so?).我的 Gridview.CSS 是否应该与我的 gridview.aspx 位于同一文件夹中(我假设是这样?)。

Here is my CSS file:这是我的 CSS 文件:

.Gridview tr.normal
 {
   background-color:white;
 }

 .Gridview tr.highlight
  {
     background-color:yellow;
  }

And here is how I am trying to implement it: In the.aspx file:这是我尝试实现它的方式:在 .aspx 文件中:

 <asp:GridView ID="MsgInbox" runat="server" ....OnRowCreated="Gridview_RowCreated" CssClass = "Gridview">

And in the C# code behind:在后面的 C# 代码中:

    protected void Gridview_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.CssClass = "highlight";
        }
    }

I know I must be missing something really simple in my C#.我知道我一定在我的 C# 中遗漏了一些非常简单的东西。 Any help would be appreciated!任何帮助,将不胜感激! Thanks!谢谢!

I stole part of my solution to this from another answer so my styling affects ALL gridviews in one shot.我从另一个答案中窃取了部分解决方案,因此我的样式会一次性影响所有网格视图。

Add this CssClass="GridView" to your asp:GridView tag.将此CssClass="GridView"添加到您的 asp:GridView 标记中。

<asp:GridView ID="grdLongID" runat="server" CssClass="GridView" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="true" OnSorting="grdLongID_Sorting" RowStyle-HorizontalAlign="center" AutoGenerateColumns="False" Width="100%">

Then in your style.css you can do things like the following:然后在您的 style.css 中,您可以执行以下操作:

table.GridView th {
  border-bottom: 1px solid #ED7A0A;
  text-decoration: none;
}

table.GridView tr:hover {
  background-color: #fabf85;
}

The key is the :hover pseudo-class.关键是:hover伪类。

First you set the Row style using this code, inside the GridView, I call it .row首先使用这段代码设置行样式,在 GridView 中,我称之为.row

<RowStyle CssClass="row"  />

then you use this css to make it change the background color, or what ever you like when the mouse is move over.然后你使用这个CSS来改变背景颜色,或者当鼠标移动到你喜欢的时候。

tr.row
{
    background-color:#fff;
}


tr.row td
{ 
}

tr.row:hover td, 
tr.row.over td
{ 
    background-color: #eee;
}

The trick here is that because GridView is rendered as table, I add the td and the tr in the style to access the mouse over the table lines.这里的技巧是因为 GridView 呈现为表格,所以我在样式中添加tdtr以访问表格行上的鼠标。

Because there is also the alternative row style AlternatingRowStyle if you like to use it, you can simple make one more style the same way.因为还有备用行样式AlternatingRowStyle如果您喜欢使用它,您可以简单地以相同的方式再制作一个样式。

Here is how I accomplished this:这是我完成此操作的方法:

Follow this tutorial to change the highlighted row on mouseover:按照本教程更改鼠标悬停时突出显示的行:

http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx

This also explains the code to process a row selection.这也解释了处理行选择的代码。 My gridview has alternating row colors.我的 gridview 具有交替的行颜色。 In order to restore the original color of the row you just hovered, create a custom attribute in mouseover for the original backgroundColor and restore it on mouseOut like so:为了恢复你刚刚悬停的行的原始颜色,在 mouseover 中为原始 backgroundColor 创建一个自定义属性,并在 mouseOut 上恢复它,如下所示:

row.Attributes["onmouseover"] = "this.originalstyle=this.style.backgroundColor;this.style.cursor='hand';this.style.backgroundColor='#ffccff';";

row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor=this.originalstyle;";

You can create hover effect using RowCreated because this will need postback for this.您可以使用 RowCreated 创建悬停效果,因为这需要回发。 You should use hover pseudo-class within you css.你应该在你的 css 中使用 hover 伪类。 Something like this像这样的东西

.Gridview tr:hover
{
  background-color:blue;
  color:white;
}

where Gridview css class applied to your Gridview Gridview css 类应用于您的 Gridview

This is how i done in my project这就是我在我的项目中所做的

CSS: CSS:

.tdonhover { background-color:#d9d9d9;important; }

<script type="text/javascript">
        $(document).ready(function () {
         $('td').hover(function () {
                $('tr').each(function () {
                    $(this).removeClass('tdonhover');
                });
                $(this).parent().addClass('tdonhover');
            });
          });

    </script>

Default.aspx page: This page contains gridview control. Default.aspx 页面:该页面包含gridview 控件。

               ` <asp:GridView ID="GridView1" runat="server" CellPadding="4" Width="940px" CssClass="gvClass gvalign" BorderColor="#E0DCD0" BorderStyle="Solid" BorderWidth="1px"
                       ForeColor="#333333" GridLines="None" >
                        <Columns>
                        <asp:TemplateField HeaderText="Srno" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="40">
                            <ItemTemplate>
                                <%# Container.DataItemIndex+1 %>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                    <RowStyle ForeColor="#603813" BackColor="#F7F6F3" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#A86E07" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#A86E07" />
                </asp:GridView>`

Using使用

<RowStyle ForeColor="#603813" BackColor="#F7F6F3" />
<AlternatingRowStyle BackColor="White" ForeColor="#A86E07" />

you can set Alternate row Background color and fontcolor您可以设置备用行背景颜色和字体颜色

This is so simple thing.这是多么简单的事情。

add the CSS in head part在头部添加 CSS

#MainContent_gvDevice tr:Hover
{  
    background-color:#F6F6F6;
}

here instead of gvDevice put your gridview id.在这里而不是gvDevice放你的 gridview id。

Better way yo can handle this mouseover using OnRowCreated你可以使用 OnRowCreated 处理此鼠标悬停的更好方法

protected void RowCreated_report(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='yellow'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                e.Row.Attributes.Add("style", "cursor:pointer;");
            }
}

I think I have the shortest and easiest solution to implement so far.到目前为止,我认为我有最短和最容易实施的解决方案。 There is no need to edit code behind or id/class names.无需编辑隐藏代码或 ID/类名称。 The only edit you need to make is adding this CSS:您需要做的唯一编辑是添加此 CSS:

tr[class^='dxgvDataRow']:hover td{ 
    background-color: #272727 !important;
}
 protected void grdDis_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            #region to highlight the grid view row 
            e.Row.Attributes.Add("onmouseover", "this.originalcolor=this.style.backgroundColor;" + " this.style.backgroundColor='#FDCB0A';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalcolor;");
            #endregion
}
}

This is for column cell hover color in the Gridview with ToolTip and ForeColor这是用于带有 ToolTip 和 ForeColor 的 Gridview 中的列单元格悬停颜色

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[2].Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.backgroundColor='aqua'";
        e.Row.Cells[2].Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='white'";
        e.Row.Cells[2].ToolTip = "You can click this cell";
        e.Row.Cells[2].ForeColor = System.Drawing.Color.Blue;
    }
}

Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM