Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

checkboxlist

Posted on 2004-11-29
5
Medium Priority
?
516 Views
Last Modified: 2008-10-03
I am using VB.net 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 ?



Moiz
0
Comment
Question by:Moizsaif123
5 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12698305
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.
            Me.DataBind()
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
        Next
        lblCheckBoxListInfo.Text = msg
    End Sub
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 12698331
Hello Moizsaif123,

Here is the sample code:

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

<html>

<head>

   <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

         Next

      End Sub

   </script>
 
</head>

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

      Select items from the CheckBoxList.

      <br><br>

      <asp:CheckBoxList id="checkboxlist1"
           AutoPostBack="True"
           CellPadding="5"
           CellSpacing="5"
           RepeatColumns="2"
           RepeatDirection="Vertical"
           RepeatLayout="Flow"
           TextAlign="Right"
           OnSelectedIndexChanged="Check_Clicked"
           runat="server">
 
         <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:CheckBoxList>
 
      <br><br>

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

</html>

Hope it helps

Thanks
0
 
LVL 5

Accepted Solution

by:
jpontani earned 400 total points
ID: 12700427
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 = "..."
sqlConn.Open()
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..."
myCheckBoxList.DataBind()

- Joe
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 12709197
Ssql = "Select * from testpurpose"
        With cmd
            .CommandText = Ssql
            .Connection = myConnection
        End With
        myConnection.Open()
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        While dr.Read
            CheckBoxList1.Items.Add(dr("ID"))
        End While
        myConnection.Close()

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....



Moiz

0
 
LVL 5

Expert Comment

by:jpontani
ID: 12709406
Dim chkBox as CheckBox
For Each chkBox In CheckBoxList1
    If(chkBox.Checked)
        Response.Write("Checkbox ID " & chkBox.ID & " is checked.")
    End If
Next

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

- Joe
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

572 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