Get all selected values of CheckBoxList in VB.NET

穿精又带淫゛_ 提交于 2019-12-11 03:15:13

问题


I've used ASP's CheckBoxList control. Now what I want is to get the all selected values in VB code.

HTML

<asp:CheckBoxList ID="chkbxlst_Users" runat="server" RepeatColumns="2" RepeatDirection="Vertical" RepeatLayout="Table"></asp:CheckBoxList>

VB

Protected Sub btnSaveSetProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveSetProject.Click
        Dim ds_selectedProjects As New DataSet
        Dim eStr As String = String.Empty
        Try

        Catch ex As Exception
            Me.ShowErrorMessage(ex.Message, "...btnSaveSetProject")
        End Try
    End Sub

On this Save button's click I want to get all the selected items' value and text in dataset.


回答1:


You can try following code:

  Protected Sub btnSaveSetProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveSetProject.Click

    Dim ds_selectedProjects As New DataSet

    Dim dt = New DataTable()
    Dim dcName = New DataColumn("Name", GetType(String))
    dt.Columns.Add(dcName)

    Dim eStr As String = String.Empty
    Try
        For Each checkBox As CheckBox In chkbxlst_Users.Items
            If (checkBox.Checked = True) Then
                Dim dr As DataRow = dt.NewRow()
                dr("ID") = checkBox.Text
                dt.Rows.Add(dr)
            End If
        Next
        ds_selectedProjects.Tables.Add(dt)
    Catch ex As Exception
        'Me.ShowErrorMessage(ex.Message, "...btnSaveSetProject")
    End Try
End Sub



回答2:


try this..

 For Each li As ListItem In chkbxlst_Users.Items

            If li.Selected Then
               // add item data into your dataset
            Else
                // do whatever you need
            End If
        End If
    Next



回答3:


Try this code        
    Dim ds_selectedProjects As New DataSet
    Dim dt_selectedProjects As New DataTable
    dt_selectedProjects.Columns.Add("Value")
    dt_selectedProjects.Columns.Add("Text")
    Dim dr As DataRow
    For i = 0 To chkbxlst_Users.Items.Count - 1
        If chkbxlst_Users.Items(i).Selected Then
            dr = dt_selectedProjects.NewRow()
            dr("Value") = Val(chkbxlst_Users.Items(i).Value)
            dr("Text") = chkbxlst_Users.Items(i).Text
            dt_selectedProjects.Rows.Add(dr)
        End If
    Next

    ds_selectedProjects.Tables.Add(dt_selectedProjects)



回答4:


Try this

   Dim str As [String] = ""
    For i As Integer = 0 To CheckBoxList1.Items.Count - 1

        If CheckBoxList1.Items(i).Selected Then

            If str = "" Then
                str = "'" + CheckBoxList1.Items(i).Value + "'"
            Else

                str += "," + "'" + CheckBoxList1.Items(i).Value + "'"

            End If
        End If
    Next
    'display in a textbox or lable with ID txtmsg
    txtmsg.Text = str


来源:https://stackoverflow.com/questions/21988757/get-all-selected-values-of-checkboxlist-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!