如何在复选框列表中保存许多选中的内容

如何在复选框列表中保存许多选中的内容

本文介绍了如何在复选框列表中保存许多选中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is my code behind







Private Sub BindCheckBoxListIssue()

        Dim dt As New DataTable()
        Dim dbconn As New SqlConnection(strCon)
        Try
            dbconn.Open()
            Dim strSql As String = "SELECT * FROM MonitorIssue"
            Dim dbcomm As New SqlCommand(strSql, dbconn)
            Dim SqlDa As New SqlDataAdapter(dbcomm)

            SqlDa.Fill(dt)
            If (dt.Rows.Count > 0) Then
                chklstIsu.DataSource = dt
                chklstIsu.DataTextField = "Issue"
                chklstIsu.DataValueField = "Issue"
                chklstIsu.DataBind()
            End If

            For i As Integer = 0 To 15
                dt.Rows(i)("IsSelected").ToString()
                chklstIsu.Items(i).Selected = Convert.ToBoolean(dt.Rows(i)("IsSelected"))
            Next

            dbconn.Close()

            setMsg(Me, "Record saved", "strKey")
        Catch
            setMsg(Me, "Record save failed", "strKey")
        End Try
    End Sub

    Private Sub UpdateIssue()
        Dim conn As New SqlConnection()
        Dim cmd As New SqlCommand
        Dim strSql = ""

        Try
            cmd.Connection = conn
            conn.Open()

            strSql = "UPDATE MonitorIssue SET IsSelected = @IsSelected WHERE Issue = @Isu"
            For Each item As ListItem In chklstIsu.Items
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@IsSelected", item.Selected)
                cmd.Parameters.AddWithValue("@ListingId", item.Value)
                cmd.ExecuteNonQuery()
            Next

            conn.Close()

            setMsg(Me, "Record saved", "strKey")
        Catch
            setMsg(Me, "Record save failed", "strKey")
        End Try
    End Sub

推荐答案

Private Sub UpdateIssue()
        Dim conn As New SqlConnection()
        Dim strSql = ""
 
        Try
            conn.Open()
 
            strSql = "UPDATE MonitorIssue SET IsSelected = @IsSelected WHERE Issue = @Isu"
            Dim cmd As New SqlCommand(strSql, conn)
            For Each item As ListItem In chklstIsu.Items
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@IsSelected", item.Selected)
                cmd.Parameters.AddWithValue("@ListingId", item.Value)
                cmd.ExecuteNonQuery()
            Next
 
            conn.Close()
 
            setMsg(Me, "Record saved", "strKey")
        Catch
            setMsg(Me, "Record save failed", "strKey")
        End Try
    End Sub





希望它有所帮助。



Hope it helps.


这篇关于如何在复选框列表中保存许多选中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 09:22