Posted on 2004-11-29
Last Modified: 2008-10-03
I am using and a webform

I want to populate a CheckBoxList with data using a query, is there an example or a website i can refer to ?

Question by:Moizsaif123
    LVL 28

    Expert Comment

    Set AutoPostBack=True to refresh on every click (cblContinent)

     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here

            If Not Page.IsPostBack Then

    ' Create a HashTable with continent names.
                Dim continents As New Hashtable(5)
                continents.Add("America", 1)
                continents.Add("Europa", 2)
                continents.Add("Asia", 3)
                continents.Add("Africa", 4)
                continents.Add("Australia", 5)

                ' Bind it to the cblContinents CheckBoxList control.
                cblContinents.DataSource = continents
                cblContinents.DataTextField = "Key"
                cblContinents.DataValueField = "Value"

                ' Perform binding for all the controls in the page.
    End If

    End Sub

     ' an element of the CheckBoxList control has been selected

        Private Sub cblContinents_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cblContinents.SelectedIndexChanged
            Dim li As ListItem, msg As String
            ' check the Selected property of each item
            For Each li In cblContinents.Items
                If li.Selected Then
                    msg &= String.Format("Item = {0}, Value = {1}<br>", li.Text, li.Value)
                End If
            lblCheckBoxListInfo.Text = msg
        End Sub
    LVL 8

    Expert Comment

    Hello Moizsaif123,

    Here is the sample code:

    <%@ Page Language="VB" AutoEventWireup="True" %>



       <script runat="server">

          Sub Check_Clicked(sender as Object, e As EventArgs)

             Message.Text = "Selected Item(s):<br><br>"

             ' Iterate through the Items collection of the CheckBoxList
             ' control and display the selected items.
             Dim i As Integer

             For i=0 To checkboxlist1.Items.Count - 1

                If checkboxlist1.Items(i).Selected Then

                   Message.Text &= checkboxlist1.Items(i).Text & "<br>"

                End If


          End Sub


       <form runat="server">
          <h3> CheckBoxList Example </h3>

          Select items from the CheckBoxList.


          <asp:CheckBoxList id="checkboxlist1"
             <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem>Item 4</asp:ListItem>
             <asp:ListItem>Item 5</asp:ListItem>
             <asp:ListItem>Item 6</asp:ListItem>

          <asp:label id="Message" runat="server"/>


    Hope it helps

    LVL 5

    Accepted Solution

    If you're using the SqlClient objects, then you can do the following:

    Dim sqlConn as New SqlConnection
    Dim sqlCommand as New SqlCommand
    Dim sqlReader as SqlDataReader
    sqlConn.ConnectionString = "..."
    sqlCommand.Connection = sqlConn
    sqlCommand.CommandText = "SELECT * FROM Table"
    sqlReader = sqlCommand.ExecuteReader
    myCheckBoxList.DataSource = sqlReader
    myCheckBoxList.DataTextField = "...field name for text..."
    myCheckBoxList.DataValueField = "...field name for values..."

    - Joe
    LVL 4

    Author Comment

    Ssql = "Select * from testpurpose"
            With cmd
                .CommandText = Ssql
                .Connection = myConnection
            End With
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While dr.Read
            End While

    above im running a query which creates a list of checkboxes....

    on server side how do i know which check box is selected or checked....


    LVL 5

    Expert Comment

    Dim chkBox as CheckBox
    For Each chkBox In CheckBoxList1
            Response.Write("Checkbox ID " & chkBox.ID & " is checked.")
        End If

    That will write the ID's of the boxes that are checked.

    - Joe

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now