如何使用vb仅在asp.net中的复选框列表中传递选中的值

时间:2013-02-12 10:34:24

标签: asp.net vb.net

我试图只将选中的值传递给数据库。但我的问题是即使是未经检查的值也会被传递。我的复选框是从下拉框动态创建的。

从下拉框创建列表框

  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim uname As String
    Dim sqlConnection As New SqlConnection(connectionString)
    sqlConnection.Open()
    Dim exe1 As String = "select USERNAME from dummy_tbl_first AS L INNER JOIN            Dummy_tbl_second AS A ON L.USER_ID= A.USER_ID INNER JOIN Dummy_tbl3 AS G ON G.GROUP_ID=A.GROUP_ID WHERE G.GROUP_NAME='" + DropDownList1.Text + "' ORDER BY USERNAME "
    Dim thisCommand As New SqlCommand(exe1, sqlConnection)
    thisCommand.ExecuteNonQuery()
    Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
    CheckBoxList1.Items.Clear()
    While (thisReader.Read())
        uname = thisReader.GetValue(0)
        CheckBoxList1.Items.Add(uname)
    End While
    thisCommand.Dispose()
    sqlConnection.Close()
    Button1.Enabled = False
End Sub

这就是我插入数据库的方式

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim id As Integer = Request.QueryString("sr_id")
    Dim first_name37 As String = CType(Session.Item("FIRST_NAME"), String)
    Session("FIRST_NAME") = first_name37
    Dim update_id As String = Request.QueryString("sr_id")

    For Each list As ListItem In CheckBoxList1.Items
        Response.Write(list.Selected)
        Dim da As Date = Date.Now()
        Dim sqlConnection As New SqlConnection(connectionString)
        sqlConnection.Open()
        Dim exe1 As String = "INSERT INTO TEST_TBL VALUES('" & id & " ','" + first_name37 + "','" + list.Value + "','" + da + "')"
        Dim thisCommand As New SqlCommand(exe1, sqlConnection)
        thisCommand.ExecuteNonQuery()
        Dim exeupdate As String = "UPDATE TEST_TBL2 SET STATUS='ASSIGNED' WHERE CR_ID='" + update_id + "'"
        Dim thisCommandUpdate As New SqlCommand(exeupdate, sqlConnection)
        thisCommandUpdate.ExecuteNonQuery()
        sqlConnection.Close()
    Next
    CheckBoxList1.Items.Clear()
    Button1.Enabled = False
End Sub

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

你需要对你的逻辑进行条件化,因为它现在根本没有区别。所有项目都在CheckBoxList1.Items中,而不仅仅是所选项目;所以,只检查所选项目......

If (list.Selected) Then
 ' proceed with this one
End If