Using javascript / css implementation GridView alternate row background color. Click the line color

Using javascript / css implementation GridView alternate row background color, click the line color

Ask Bo a few days ago was found in someone clicks GridView question about the issue of line color, suddenly think of a long, long time ago, wrote an article in a simple but commonly used table style - mouse across the color line - simple implementation, is about Alternating table row color and change color when the mouse point, and just today put a complement and extend it about, together with the click of a mouse selection (in fact, just click a color change, "select" is inappropriate here), so as to put this on direct application to the GridView, if other controls, directly or HTML, a little modification can also be used here only to provide a train of thought. While the GridView to use the alternate line AlternatingRowStyle provide background color, but this thing really in a bad mood with, to watch it to generate HTML that table, called a chaos ah.

The following is a code, a more detailed note should be still more suitable for beginners, you can put the following two files to copy the code directly to your direct execution of projects. Bottom there is file download address, download directly after the operation, code in IE7 and Firefox2 test is passed, have any questions, please post below, I will try to reply promptly.

BackgroundColor.aspx
Main contains a GridView, is the focus of our torment, have a bunch of javascript, is to toss us a means of GridView
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "BackgroundColor.aspx.cs" Inherits = "_BackgroundColor"%>

<! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> BackgroundColor </ title>
</ head>
<body>
<form runat="server">
<asp:GridView runat="server"> </ asp: GridView>
</ form>
<script type="text/javascript">
/ / Put on onload in case, because if I do not know JS wrote here directly, such as the page is finished loading before it is implemented / / using the GridView <%=%> output because the ID is some cases (such as using MasterPage) will cause changes in HTML in the ID / / recommend the use of color values of Hex, such as # ff0000 or # f00
window.onload = function () (
GridViewColor ("<%= gvMeiMingZi.ClientID %>","# fff "," # eee "," # 6df "," # fd6 ");
)

/ / Parameters followed (the latter two, if designated as a null value, then the case will not happen):
/ / GridView ID, the normal line background color, background color of alternate lines, mouse, point to line the background color, mouse, click the back view function GridViewColor (GridViewId, NormalColor, AlterColor, HoverColor, SelectColor) (
/ / Want to control access to all rows var AllRows = document.getElementById (GridViewId). GetElementsByTagName ( "tr");

/ / Set the background color of each line and the case, one cycle from the beginning rather than 0, you can avoid the first line in table for (var i = 1; i <AllRows.length; i + +) (
/ / Set the default background color of the Bank AllRows. Style.background = i% 2 == 0? NormalColor: AlterColor;

/ / If the specified mouse point to the background color, then add the onmouseover / onmouseout events / / status in selected lines of these two incidents happened not to change the color if (HoverColor!
= "") (
AllRows. Onmouseover = function () (if (! This.selected) this.style.background = HoverColor;)
if (i% 2 == 0) (
AllRows. Onmouseout = function () (if (! This.selected) this.style.background = NormalColor;)
)
else (
AllRows. Onmouseout = function () (if (! This.selected) this.style.background = AlterColor;)
)
)

/ / If you specify a background color of the mouse click, then add the onclick event / / in the case have been modified in response to clicks in the selected line if (SelectColor! = "") (
AllRows. Onclick = function () (
this.style.background = this.style.background == SelectColor? HoverColor: SelectColor;
this.selected =! this.selected;
)
)
)
)
</ script>
</ body>
</ html>

BackgroundColor.aspx.cs
Used to generate a pile of data for testing, or in front of the GridView has No , it can not see the effect of the
using System;
using System.Data;

public partial class _BackgroundColor: System.Web.UI.Page
(
protected void Page_Load (object sender, EventArgs e)
(
/ / Generate DataTable and add 10 out DataTable dt = new DataTable ();
for (int i = 0; i <10; i + +)
(
dt.Columns.Add ();
)

/ / DataTable to add 20 lines of data for (int i = 0; i <20; i + +)
(
dt.Rows.Add (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
)

/ / Bind the DataTable to the GridView
gvMeiMingZi.DataSource = dt;
gvMeiMingZi.DataBind ();
)
)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Using javascript / css implementation GridView alternate row background color. Click the line color

  • Hibernate primary key strategy-sequence

    Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...

  • hibernate call stored procedure

    hibernate call stored procedure

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.1 ...

  • hibernate generic generic DAO

    hibernate generic generic DAO

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries