现在我有个grildview 帮定一列,我想点击一行,这行颜色变掉,其余颜色还原。 e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ffe300'");这样写的话,不会还原,请问怎么写?

解决方案 »

  1.   

    DEMO代码,代码有一定的优化空间,自己解决吧。
    -cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Default8 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = GenerateTable();
            GridView1.DataBind();
        }    private DataTable GenerateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Products", typeof(string));
            dt.Columns.Add("Price", typeof(decimal));
            DataRow row;
            Random rnd = new Random();
            for (int i = 1; i != 31; ++i)
            {
                row = dt.NewRow();
                row[0] = i;
                row[1] = "Product_" + i;
                row[2] = Math.Round(rnd.Next(20, 100) / 1.48D, 2);
                dt.Rows.Add(row);
            }
            return dt;
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ffe300';onRowClick(this)");
        }
    }
    .aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %><!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 runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            </asp:GridView>
        </div>
        </form>
        <script type="text/javascript">
        function onRowClick(srcRow){
            var rows=document.getElementById("<%=GridView1.ClientID %>").rows;
            for(var i=0;i!=rows.length;++i)
                rows[i].style.backgroundColor="#fff";
            srcRow.style.backgroundColor="#ffe300";
        }
        </script>
    </body>
    </html>