javascript代码:
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
function checkme(obj,rowid)
{
if(obj.checked)
{
rowid.style.backgroundColor='silver';
}
else
{
rowid.style.backgroundColor='';
}
}
< / script>
后台主要代码:
function checkme(obj,rowid)
{
if(obj.checked)
{
rowid.style.backgroundColor='silver';
}
else
{
rowid.style.backgroundColor='';
}
}
< / script>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("select [id],[name] from [information]", conn);
DataSet dataset = new DataSet();
adp.Fill(dataset, "information");
conn.Close();
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
GridView1.Rows[i].Attributes.Add("id", "row" + i.ToString());
chbox.Attributes.Add("onclick", "checkme(this,row" + i.ToString() + ");");
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("select [id],[name] from [information]", conn);
DataSet dataset = new DataSet();
adp.Fill(dataset, "information");
conn.Close();
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
GridView1.Rows[i].Attributes.Add("id", "row" + i.ToString());
chbox.Attributes.Add("onclick", "checkme(this,row" + i.ToString() + ");");
}
}
}
只适用于GridView行背景没有设置颜色的操作!!!
没有考虑当GridView行中原本有颜色时怎样实现恢复原来颜色