Implementing Hover Effect on Elements within Item template of GridView

I want to update the background color of GridView rows when the mouse hovers over them. It's working fine for columns within <boundfield>, but the background color of labels inside <itemtemplate> does not change on MouseHover.

This is how my Gridview looks:

<asp:GridView ID="gvStudentTraining" runat="server" AutoGenerateColumns="False" Width="100%" ShowFooter="true" CssClass="mydatagrid" HeaderStyle-CssClass="header" PagerStyle-CssClass="pager" RowStyle-CssClass="rows" OnPageIndexChanging="gvStudentTraining_PageIndexChanging" OnRowDataBound="gvStudentTraining_RowDataBound">
    <Columns>

        <asp:BoundField DataField="TS_TrainingLocation" HeaderText="University" SortExpression="University">
            <HeaderStyle HorizontalAlign="Center" Wrap="false" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

        <asp:BoundField DataField="TS_TrainingName" HeaderText="Training Name" SortExpression="Training Name">
            <HeaderStyle HorizontalAlign="Center" Wrap="false" />
            <ItemStyle HorizontalAlign="Center" Wrap="false" />
        </asp:BoundField>

        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label CssClass="rownumber" ID="Total" Text='<%#Eval("Total")%>' runat="server" />
            </ItemTemplate>
            <HeaderStyle Wrap="false" />
            <ItemStyle HorizontalAlign="Center" Wrap="False" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Here are some CSS Styles for rows and hover:

<style>
    .rows {
        background-color: #fff;
        font-family: Arial;
        font-size: 14px;
        color: #000;
        min-height: 25px;
        text-align: left;
    }

        .rows:hover td {
            background-color: #5badff;
            color: #fff;
        }

    .rownumber:hover {
        background-color: #5badff;
        color: #fff;
    }

    .mydatagrid a /** FOR THE PAGING ICONS **/ {
        background-color: Transparent;
        padding: 5px 5px 5px 5px;
        color: #fff;
        text-decoration: none;
        font-weight: bold;
    }

        .mydatagrid a:hover /** FOR THE PAGING ICONS HOVER STYLES**/ {
            background-color: #000;
            color: #fff;
        }

    .pager span /** FOR THE PAGING ICONS CURRENT PAGE INDICATOR **/ {
        background-color: #fff;
        color: #000;
        padding: 5px 5px 5px 5px;
    }
</style>

I've attempted various solutions, including the following:

protected void gvStudentTraining_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string onmouseoverStyle = "this.style.backgroundColor='#5badff'";
        string onmouseoutStyle = "this.style.backgroundColor='white'";
        if (e.Row.HasControls())
        {
            Label temp = (Label)e.Row.FindControl("lblTotal");
            temp.Attributes.Add("onmouseover", onmouseoverStyle);
            temp.Attributes.Add("onmouseout", onmouseoutStyle);
        }
    }
}

Can anyone suggest a way to change the background-color of a row, including the labels inside <itemtemplate>, when hovering the mouse over that row? Thank you in advance.

Answer №1

The issue lies in the '.mydatagrid span` you are using for pagination. Since a Label Control also renders a span element, it is inheriting the same styles.

To resolve this problem, you can:

.pager span /** STYLING FOR THE PAGING ICONS CURRENT PAGE INDICATOR **/ {
    background-color: #fff;
    color: #000;
    padding: 5px 5px 5px 5px;
 }

Alternatively, switching from a Label to a Literal control will also solve the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a dynamic update of an HTML element's content with JSON data - Learn how!

My task involves creating a quiz application where I need to show the answers along with images of the choices stored in my JSON data. However, I encounter an error: Uncaught TypeError: Cannot set properties of null (setting 'src') when I attempt ...

"Manipulating DOM Elements: Create and Delete Cloned Divs with

I successfully cloned a dropdown with a unique ID on my website. Now, I'm struggling to implement a "x" button that will remove the added clones. Here is the javascript code I'm using: var counter = 1; function addInput(divName, template){ ...

Identifying Hashtags with Javascript

I am trying to identify hashtags (#example) in a string using javascript and convert them to <a href='#/tags/example'>example</a> Currently, I have this code: var text = '#hello This is an #example of some text'; text.r ...

Looking to showcase initial API data upon page load without requiring any user input?

Introduction Currently, I am retrieving data from the openweatherAPI, which is being displayed in both the console and on the page. Issue My problem lies in not being able to showcase the default data on the frontend immediately upon the page's fir ...

How can we pass the onClick prop from a child component to a parent component in React with Typescript?

Currently, I am utilizing React along with TypeScript. I am curious about the process of passing an event from the parent component to a child component using props. Here is an example for better understanding: parent.tsx const ParentComponent: React.F ...

Using seleniumjs to ensure that the element is ready for user input before proceeding

Currently, my application is in a state where it needs to wait for an iframe using the isElementPresent method before switching to it. The issue arises when I encounter trouble within the iFrame itself. I need to ensure that an input component within the ...

Transitioning into a fade out effect using CSS

I successfully created a modal using CSS and jQuery. The fade in transition works perfectly, but I'm having trouble implementing a transition for when it fades out and scales back to 1.4. The main issue is that the modal disappears too quickly. Chec ...

the draggable element turns into nothing

I have created a task manager based on a tutorial I found online. After following the tutorial, I decided to add more functionality by allowing users to add tasks when clicking on a button. Everything works fine until I try to drag one of the added element ...

AngularJS checkbox validation requires a minimum of two checkboxes to be selected

When using AngularJS, I am looking to create a validation rule where a minimum of 2 checkboxes must be checked for the input to be considered valid. Here is what I have attempted: <div ng-repeat="item in items"> <label><input type="chec ...

Tips for Saving JSON Response from Fetch API into a JavaScript Object

I am facing an issue trying to store a Fetch API JSON as a JavaScript object in order to use it elsewhere. The console.log test is successful, however I am unable to access the data. The Following Works: It displays console entries with three to-do items: ...

Tips for creating an infinite repeating loop in jQuery

I'm attempting to create a jQuery infinite loop featuring images within a div. I've experimented with using the setInterval function, but encountered an issue where the animation starts after a 3-second delay and does not seamlessly repeat itself ...

What could be causing my media query to not function properly?

I've been experimenting with some basic media queries but am encountering an issue with the div element that has the class "container." Whenever I adjust the screen size to be between 992px and 1200px, the div disappears from the page. It's perpl ...

Using CSS to append additional text to the end of a hyperlink

I have scoured the depths of the web, yet no solution has revealed itself to me. I am on the hunt for a snippet of CSS that can append additional text to the end of a URL. For instance - www.google.com/search-url I am seeking a magical snippet that can ...

Designing an uncomplicated password authentication system without embedding the password itself [Using PHP, Javascript, and MySQL]

I'm in the process of setting up a basic login prompt for my local website. I initially experimented with Javascript, but I'm looking for a way to avoid hardcoding the password. Users receive their passwords via email, so there's no need for ...

Can you confirm if the user originated from JavaScript when Ajax is sent?

Illustration: Upon using "ajax.send", the file is displayed (for example, "post.php?q=...".) However, by copying the specific "url" and pasting it into the browser with the same parameters, access can also be gained. Hence, is there a way to prevent this ...

Merging an AppBar and Drawer in Material UI for a seamless user interface design

I am working on integrating an AppBar component with a drawer feature. Here is the code for the AppBar: import React from "react"; import PropTypes from "prop-types"; import { withStyles } from "material-ui/styles"; import AppBar from "material-ui/AppBar" ...

The CSS code designed to limit the display to only a two-line sentence is failing to work properly in Internet Explorer

I've implemented the following CSS code to ensure that the display sentence doesn't exceed 2 lines, with the remaining text being replaced by "...". .bell-notification-max-words-display { overflow: hidden; display: -webkit-box; ...

Tips for excluding files in a webpack configuration for a Vue application during the production build

I am attempting to remove an Html file named "dev.html" from the final product build. What configurations do I need to make in webpack for this? I understand that rules need to be applied, but where exactly do I need to configure them? Below is a snippe ...

Reading JavaScript files using the HTML 5 File Reader

Currently, I am working on a feature that allows users to drag and drop a folder containing JavaScript files into an HTML5 page. Here is the code snippet for my implementation: $scope.files = []; //Establish dropzone var dropbox; dropbox = document.getEle ...

Display PHP array information in an HTML table

I am facing an issue with an array that contains a record set generated by the CodeIgniter model. When attempting to display these records in an HTML table using my view, the layout is not rendering correctly. Here is a snippet of my view: <html> & ...