GridView与CheckBox结合(VB.NET)

1.前台CheckBoxDefault.aspx.vb

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CheckBoxDefault.aspx.vb" Inherits="CheckBoxDefault" %> CheckBoxDefault使用

  

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("") Exit Sub End If sqlCon = New SqlConnection(strCon) sqlCom = New SqlCommand(strSql, sqlCon) Try sqlCon.Open() sqlCom.ExecuteNonQuery() Response.Write("") Catch ex As Exception sqlCon.Close() End Try Bind() End Sub End Class

你可能感兴趣的:(vb.net)