When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Data Warehouse Architect (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

User Tips: Highlighting a DataGrid Row When Mousing Over


By Mark Mailer

I recently read Scott Mitchell's article Creating a Row-Selectable DataGrid Control, which details how to create a custom DataGrid server control whose rows' background colors change when moused over and whose rows can be selected by clicking anywhere in the row. (You can see a live demo of Scott's control here.)

Rather than use Scott's custom control, I wanted a quick and easy way to add row-level highlighting when mousing over a particular DataGrid in a particular ASP.NET page. My solution was to directly add onmouseover and onmouseout attributes to the DataGrid's items in the ItemDataBound event handler. My code is used in an intranet, IE-only setting and has worked well. While not as robust as Scott's approach, it can be quickly and easily implemented.

Anyway, the code for the DataGrid's ItemDataBound event handler is provided below:

Private Sub DataGridID_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGridID.ItemDataBound

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

        ' Do the mouseover and mouseout javascript
        Dim oBGcolour As String

        If e.Item.ItemType = ListItemType.Item Then
            oBGcolour = Right(Hex(DataGridID.ItemStyle.BackColor.ToArgb()), 6)
        ElseIf e.Item.ItemType = ListItemType.AlternatingItem Then
            oBGcolour = Right(Hex(DataGridID.AlternatingItemStyle.BackColor.ToArgb()), 6)
        End If

        e.Item.Attributes.Add("onmouseover", "this.style.background='#cdcdcd';")
        e.Item.Attributes.Add("onmouseout", "this.style.background='#" & oBGcolour & "';")

    End If

End Sub

Happy Programming!

  • By Mark Mailer

    Return to user tips...


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article


  • The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers