如何使用gridview vb.net中的复选框更新数据库列

时间:2013-05-02 10:50:59

标签: asp.net vb.net visual-studio visual-studio-2008

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

我使用数据库列活动绑定复选框...

当我单击复选框时,默认情况下会选中它,因为dbase列中的active的默认值为1 ..

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

我有一个代码片但不能正常工作

 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

0 个答案:

没有答案