Highlighting Rows in an ASP.Net DataGrid...

原创 2004年10月06日 18:41:00
Highlighting Rows in an ASP.Net DataGrid... By: John Kilgo spacer.gif Date: January 11, 2003spacer.gifdownload.gifspacer.gifDownload the code. spacer.gif <?XML:NAMESPACE PREFIX = ASP />spacer.gifPrinter Friendly Version

I like the idea of highlighting rows in a DataGrid as the user scrolls the mouse up and down over the grid. The effect can be achieved using several lines of client-side JavaScript, but why do that when two lines of .Net code will accomplish the same thing? The trick is to implement an ItemDataBound event handler and add OnMouseOver and OnMouseOut attributes to handle the color change.

The code for this project is in two parts. An aspx page is used to define the datagrid and its attributes, while a .vb code-behind page is used to implement the database activity and handle the ItemDataBound method. First the .aspx page.

Notice the line OnItemDataBound="dtgCustomers_ItemDataBound". OnItemDataBound is a DataGrid method that raises the ItemDataBound event. "dtgCustomers_ItemDataBound" is the event handler which is contained in the code-behind file. One note: I chose to display rows from the NorthWind Customers table. Since this table contains over 90 rows, I also implemented paging for the datagrid. If you have used datagrids at all the rest of the attributes should be self explantory. I won't spend any more time on the .aspx page.

<%@ Page Language="vb" Src="HighlightDataGrid.aspx.vb" Inherits="HighlightDataGrid" %>

<html>
<head>
<title>Mouseover Highlighting of DataGrid Rows</title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:DataGrid id="dtgCustomers"
              runat="server"
              OnItemDataBound="dtgCustomers_ItemDataBound"
              CellPadding="4"
              BackColor="White"
              BorderWidth="1px"
              BorderStyle="None"
              BorderColor="Indigo"
              AllowPaging="True"
              PageSize="10"
              OnPageIndexChanged="dtgCustomers_PageIndexChanged"
              PagerStyle-Mode="NumericPages"
              EnableViewState="False"
              AutoGenerateColumns="True">
  <SelectedItemStyle font-bold="True"
                     forecolor="#663399"
                     backcolor="#FFCC66">
  </SelectedItemStyle>
  <ItemStyle forecolor="Navy"
             backcolor="White">
  </ItemStyle>
  <AlternatingItemStyle ForeColor="Navy"
                        BackColor="Yellow">
  </AlternatingItemStyle>
  <HeaderStyle font-bold="True"
               forecolor="Navy"
               backcolor="LightYellow">
  </HeaderStyle>
  <PagerStyle horizontalalign="Center"
              forecolor="Navy"
              backcolor="LightYellow">
  </PagerStyle>
</asp:DataGrid>
</form>
</body>
</html>
Now for the codebehind file. The majority of the code is the typical database stuff to get the data, and to bind the data to the grid. All you see just below should again be self explanatory.
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Imports System.Data

Public Class HighlightDataGrid : Inherits Page

  Protected dtgCustomers As DataGrid

  Sub Page_Load(Sender As Object, E As System.EventArgs)
    BindTheGrid()
  End Sub

  Sub BindTheGrid()
    Dim sqlConn As New SqlConnection( _
      "server=localhost;database=NorthWind;uid=sa;pwd=secret")
    Dim dtaCustomers As New SqlDataAdapter( _
        "Select CustomerID, CompanyName, ContactName, Phone " _
        & "From Customers", sqlConn)
    Dim dsCustomers As DataSet

    Try
      dsCustomers = New DataSet()
      dtaCustomers.Fill(dsCustomers)
      dtgCustomers.DataSource = dsCustomers
      dtgCustomers.DataBind()
    Catch SQLEx As SqlException
      Response.Write(SQLEx.Message.ToString())
    Catch Ex As Exception
      Response.Write(Ex.Message.ToString())
    Finally
      sqlConn.Close()
    End Try
  End Sub
The code immediately below is where all the work gets done to implement the highlighting. The line within the first If block simply adds the OnMouseOver attribute to the Item changing the background color no matter whether we are in an ItemTemplate or an AlternatingItemTemplate. In the If-Then-Else block we are checking to see if we are in an ItemTemplate or an AlternatingItemTemplate and using the OnMouseOut attribute to set the background color of the row back to its original setting.
  Sub dtgCustomers_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
      e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'")
    End If
    If e.Item.ItemType = ListItemType.Item Then
      e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='White'")
    Else
      e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='Yellow'")
    End If
  End Sub

  Sub dtgCustomers_PageIndexChanged(sender As Object, e As DataGridPageChangedEventArgs)
    dtgCustomers.CurrentPageIndex = e.NewPageIndex
    BindTheGrid()
  End Sub
End Class
You may run the program by clicking Here.
You may download the code by clicking Here.

Multi-row Editing in the ASP.NET DataGrid...

Multi-row Editing in the ASP.NET DataGrid... This articles demonstrates how you can have multiple da...
  • hiyaolee
  • hiyaolee
  • 2004年10月06日 18:37
  • 878

HighlightingSystem(边缘发光插件)的简单使用(一)

本人使用的是Unity5.3.5,关于这个插件我在网上找到了几个版本,本人使用的可能是旧版本,不过用起来效果没差,接下来就简单的谈谈这个插件的使用。 PS:http://pan.baidu.com...
  • IT_break
  • IT_break
  • 2017年02月10日 11:03
  • 1590

HighLighting System插件的使用以及一些问题

笔者使用的是V4.0版本,使用起来十分方便 1.给需要产生高亮效果的物体添加Highlighter脚本 2.给摄像机添加HighlightingRenderer脚本 3.自己写控制脚本,调用Highl...
  • dly215011
  • dly215011
  • 2016年09月09日 09:41
  • 2477

ASP.NET - Table、TableRow 和 TableCell Web 服务器控件概述

       Table Web 服务器控件使您能够在 ASP.NET 页上创建可使用服务器代码进行编程的表。TableRow 和 TableCell Web 服务器控件提供显示 Table 控件的内...
  • dodream
  • dodream
  • 2009年11月25日 16:24
  • 4737

Highlighting System v4.0(Unity边缘高亮插件)

使用很简单:主相机上挂上HighlightingRenderer插件在有高亮需求的物体身上挂上如下截图中的任意一个均可 FR:海涛高软(QQ技术群:386476712)...
  • qq_15267341
  • qq_15267341
  • 2016年12月02日 13:20
  • 3425

How to Confirm a Delete in an ASP.NET Datagrid...

If you are allowing users to delete rows from a datagrid, you may want to give them the chance to co...
  • hiyaolee
  • hiyaolee
  • 2004年10月06日 21:00
  • 951

Unity3D插件之Highlighting System:物件高亮插件

需求:点选物体时,高亮显示,以示用户选中了这个物件。 官方教程:http://deepdreamgames.com/highlighting-system/  翻译整理如下: 1、...
  • u013692049
  • u013692049
  • 2016年10月30日 22:23
  • 3804

Mat 里面的 cols 和 rows

cols 是 列数 相当于 width 对应 .x rows 是 行数 相当于 height 对应 .y
  • Scythe666
  • Scythe666
  • 2014年09月04日 09:41
  • 2597

syntax highlighting

Since iOS 5 you can use a UIWebView and set contenteditable="true" on a div in it. This makes the ...
  • gnicky
  • gnicky
  • 2013年10月06日 21:51
  • 614

HTML页面中rows="119,*,99"表示什么意思?

HTML页面中rows="119,*,99"表示什么意思?
  • anya
  • anya
  • 2010年10月27日 11:11
  • 3458
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Highlighting Rows in an ASP.Net DataGrid...
举报原因:
原因补充:

(最多只允许输入30个字)