0

我在gridview行中有一个复选框......并且有一个数据库列名称“Active”,其默认值为“1”

我将复选框与激活的数据库列绑定...

当我默认单击复选框时,它被选中,因为 dbase 列中活动的默认值为 1 ..

我希望当我取消选中复选框时,所选行的活动列上更新的值将为 0...并且复选框保持未选中状态...。

我有一个代码 sippet 但不工作

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        For Each myRow As GridViewRow In GridView1.Rows
            'Find the checkbox
            Dim Chk1 As CheckBox = DirectCast(myRow.FindControl("CheckBox1"), CheckBox)
            Dim id As HiddenField = DirectCast(myRow.FindControl("HiddenField1"), HiddenField)
            Dim active As HiddenField = DirectCast(myRow.FindControl("HiddenField3"), HiddenField)
            Dim SQLData As New MySql.Data.MySqlClient.MySqlConnection("server=localhost;User Id=root;Persist Security Info=True;database=clixngo")
            Dim cmdSelect As New MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM my_state where id='" & id.Value & "'", SQLData)
            SQLData.Open()
            Dim dtrReader As MySql.Data.MySqlClient.MySqlDataReader = cmdSelect.ExecuteReader()
            If dtrReader.HasRows Then
                While dtrReader.Read()
                    Chk1.Checked = dtrReader("active")
                End While
            Else
            End If
            dtrReader.Close()
            SQLData.Close()

        Next
    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each myRow As GridViewRow In GridView1.Rows
            Dim Chk1 As CheckBox = DirectCast(myRow.FindControl("CheckBox1"), CheckBox)
            Dim id As HiddenField = DirectCast(myRow.FindControl("HiddenField1"), HiddenField)
            Dim active As HiddenField = DirectCast(myRow.FindControl("HiddenField3"), HiddenField)
            If Chk1.Checked = True Then
                SqlDataSource1.UpdateParameters("id").DefaultValue = id.Value
                SqlDataSource1.UpdateParameters("active").DefaultValue = "1"
                SqlDataSource1.UpdateParameters("state").DefaultValue = "Andaman and Nicobar"
                SqlDataSource1.Update()
            ElseIf Chk1.Checked = False Then
                SqlDataSource1.UpdateParameters("id").DefaultValue = id.Value
                SqlDataSource1.UpdateParameters("active").DefaultValue = "0"
                SqlDataSource1.UpdateParameters("state").DefaultValue = "Andaman and Nicobar"
                SqlDataSource1.Update()
            End If
        Next
    End Sub
4

0 回答 0