JS部分: // 全选 function CheckAll() ...{ var GridView1 = document.getElementById("<%=GridView1.ClientID %>"); for(i = 1;i < GridView1.rows.length; i++) ...{ GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = true; GridView1.rows[i].className= "TDC"; } }// 取消选择 function CancelAll() ...{ var GridView1 = document.getElementById("<%=GridView1.ClientID %>"); for(i = 1;i < GridView1.rows.length; i++) ...{ GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = false; GridView1.rows[i].className= "TDCNONE"; } }// 反选 function FanCheckAll() ...{ var GridView1 = document.getElementById("<%=GridView1.ClientID %>"); for(i = 1;i < GridView1.rows.length; i++) ...{ if(GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked == true) ...{ GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = false; GridView1.rows[i].className= "TDCNONE"; } else ...{ GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = true; GridView1.rows[i].className= "TDC"; } } }// 选种行变换 function Tcheck(obj) ...{ var objin=obj.parentElement.parentElement; if(objin.className== "TDC") objin.className= "TDCNONE" else objin.className= "TDC" } C#部分: using System;using System.Data;using System.Data.SqlClient;using System.Configuration;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 _Default : System.Web.UI.Page ...{ protected void Page_Load(object sender, EventArgs e) ...{ if (!IsPostBack) ...{ BindView(); } } private void BindView() ...{ SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); DataSet ds = new DataSet(); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("select ProductID,ProductName,UnitPrice=convert(decimal(10,2),UnitPrice),UnitsOnOrder from Products", conn); sda.Fill(ds); conn.Close(); GridView1.DataSource = ds; GridView1.DataBind(); foreach (GridViewRow gr in GridView1.Rows) ...{ CheckBox cb = (CheckBox)gr.FindControl("CheckBox1"); cb.Attributes["onclick"] = "Tcheck(this)"; } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) ...{ GridView1.EditIndex = e.NewEditIndex; BindView(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) ...{ GridView1.EditIndex = -1; BindView(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) ...{ int prdID = int.Parse(GridView1.DataKeys[e.RowIndex]["ProductID"].ToString()); string prdName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.ToString(); float prdprice = float.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text); int prdNumber = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text.ToString()); string prdSql = "UPDATE Products SET ProductName= '" + prdName + "',UnitPrice=" + prdprice + ",UnitsOnOrder=" + prdNumber + " WHERE ProductID=" + prdID; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); conn.Open(); SqlCommand com = new SqlCommand(prdSql, conn); com.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; BindView(); } protected void Button1_Click(object sender, EventArgs e) ...{ foreach (GridViewRow gr in GridView1.Rows) ...{ CheckBox cb = (CheckBox)gr.FindControl("CheckBox1"); cb.Checked = true; } } protected void Button2_Click(object sender, EventArgs e) ...{ foreach (GridViewRow gr in GridView1.Rows) ...{ CheckBox cb = (CheckBox)gr.FindControl("CheckBox1"); if (cb.Checked == true) ...{ int prdID = int.Parse(gr.Cells[1].Text); string prdSql = "DELETE FROM Products WHERE ProductID=" + prdID; Response.Write(prdSql); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); conn.Open(); SqlCommand com = new SqlCommand(prdSql, conn); com.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; BindView(); } } }}