1.前台CheckBoxDefault.aspx.vb <%@ Page Language="VB" AutoEventWireup="false" CodeFile="CheckBoxDefault.aspx.vb" Inherits="CheckBoxDefault" %> <!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>CheckBoxDefault使用</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="False" AllowPaging="True" PageSize="20"> <Columns> <asp:TemplateField > <ItemTemplate > <asp:CheckBox ID = "CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="EmpNo" HeaderText="社员号"/> <asp:BoundField DataField="EmpNm" HeaderText ="姓名" /> <asp:BoundField DataField="OAddr" HeaderText="地址" /> </Columns> <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> <AlternatingRowStyle BackColor="#F7F7F7" /> </asp:GridView> <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack ="True" Font-Size="9pt" Text="全选" /> <asp:Button ID="BtnCancel" runat="server" Font-Size = "9pt" Text = "取消" /> <asp:Button ID="BtnDel" runat="server" Font-Size = "9pt" Text = "删除" /> </div> </form> </body> </html> 2.后台代码 Imports System.Data.SqlClient Imports System.Data.DataSet Partial Class CheckBoxDefault Inherits System.Web.UI.Page Dim sqlCon As SqlConnection Dim sqlCom As SqlCommand Const strCon As String = "Data Source = 192.168.11.53;DataBase = NorthWind; Uid = sa;Pwd = m1erpsql" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Bind() End If End Sub '数据绑定 Public Sub Bind() Dim strSql As String Dim da As SqlDataAdapter Dim ds As New Data.DataSet strSql = " SELECT EmpNO,EmpNM,OAddr FROM NorthWind..Hant" sqlCon = New SqlConnection(strCon) da = New SqlDataAdapter(strSql, sqlCon) Try sqlCon.Open() da.Fill(ds, "NorthWind..Hant") GridView1.DataSource = ds GridView1.DataBind() Catch ex As Exception sqlCon.Close() End Try End Sub '页码转换函数 Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging GridView1.PageIndex = e.NewPageIndex Bind() End Sub '全选 Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged Dim i As Integer Dim cBox As CheckBox For i = 0 To GridView1.Rows.Count - 1 'cBox类定义 cBox = CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox) If (CheckBox2.Checked = True) Then cBox.Checked = True Else cBox.Checked = False End If Next End Sub '全部取消 Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click Dim i As Integer Dim cBox As CheckBox CheckBox2.Checked = False For i = 0 To GridView1.Rows.Count - 1 cBox = CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox) cBox.Checked = False Next End Sub '删除 Protected Sub BtnDel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDel.Click Dim strSql As String Dim i As Integer Dim cBox As CheckBox strSql = "" For i = 0 To GridView1.Rows.Count - 1 cBox = CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox) If cBox.Checked = True Then strSql = strSql & " DELETE FROM NorthWind..Hant WHERE EmpNO = '" & GridView1.Rows(i).Cells(1).Text.ToString().Trim() & "'; " End If Next MsgBox(strSql) If strSql = "" Then Response.Write("<mce:script type="text/javascript"><!-- alert('请确认要删除的字符串!') // --></mce:script>") Exit Sub End If sqlCon = New SqlConnection(strCon) sqlCom = New SqlCommand(strSql, sqlCon) Try sqlCon.Open() sqlCom.ExecuteNonQuery() Response.Write("<mce:script type="text/javascript"><!-- alert('删除记录成功!') // --></mce:script>") Catch ex As Exception sqlCon.Close() End Try Bind() End Sub End Class