【问题标题】:How to avoid duplicate items being added from one CheckBoxList to another CheckBoxList (Asp.net vb.net)如何避免重复项从一个 CheckBoxList 添加到另一个 CheckBoxList (Asp.net vb.net)
【发布时间】:2014-02-25 00:02:54
【问题描述】:

我有 5 个复选框列表。每个复选框里面有 6 个复选框(下面是我的数据库)

checkboxlist1 : 行

checkboxlist2:rowb

checkboxlist3:rowc

checkboxlist4:rowd

checkboxlist5:rowe

id:整数, 罗瓦:位, 行:位, 行:位, 行:位, 罗:位,

rowtext:nvarchar(50)

我的问题是,将项目从一个 CheckBoxList 添加到另一个 CheckBoxList 时

,如何检查item是否已经存在于第二、第三、第四、第五个CheckBoxLists中?

这是我的代码

    Protect sub_button1

'一个复选框列表 对于 As Integer = 0 To CheckBoxList1.Items.Count - 1

        If CheckBoxList1.Items(a).Selected Then
            rowtext = CheckBoxList1.Items(a).Text
            rowa = 1
            rowb = 0
            rowc= 0
            rowd= 0

            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd,) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

    'B CheckBoxList
    For b As Integer = 0 To CheckBoxList2.Items.Count - 1

        If CheckBoxList2.Items(b).Selected Then
            rowtext = CheckBoxList2.Items(b).Text
             rowa = 0
            rowb = 1
            rowc= 0
            rowd= 0
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

'C CheckBoxList For c As Integer = 0 To CheckBoxList3.Items.Count - 1

        If CheckBoxList3.Items(c).Selected Then
            rowtext = CheckBoxList3.Items(c).Text
             rowa = 0
            rowb = 0
            rowc= 1
            rowd= 0
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

'D CheckBoxList For d As Integer = 0 To CheckBoxList4.Items.Count - 1

        If CheckBoxList4.Items(d).Selected Then
            rowtext = CheckBoxList4.Items(d).Text
             rowa = 0
            rowb = 0
            rowc= 0
            rowd= 1
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next



    Catch ex As Exception


    End Try

End Sub

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    在我将它们放入数据库之前,我会使用递归循环来检查所有框。

    我可以在几分钟内写出代码..

         Dim listofCheckBoxLists As New List(Of CheckedListBox)
    'first to gather the info
    For i = 1 To 4
        Dim checkboxlists As CheckedListBox = Me.Controls.Find("CheckedListBox" & i, True)(0)
        listofCheckBoxLists.Add(checkboxlists)
    Next
    
    'now to go through it all
    For a = 0 To listofCheckBoxLists.Count - 1
        For b = 0 To listofCheckBoxLists(a).Items.Count - 1
            For c = a To listofCheckBoxLists.Count - 1
                For d = 0 To listofCheckBoxLists(c).Items.Count - 1
                    If listofCheckBoxLists(a).Items(b) = listofCheckBoxLists(c).Items(d) Then
                        '''here should be where if they are the same you can do something
                    End If
                Next
            Next
        Next
    Next
    

    【讨论】:

    • 感谢您的回答,但我正在 Web 表单上编码,我只想显示不重复的值,我该怎么做?
    • 那么它的 me.findcontrol 和你的副本将在 if 语句中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    相关资源
    最近更新 更多