Moiz Saifuddin
asked on
checkboxlist
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
I want to populate a CheckBoxList with data using a query, is there an example or a website i can refer to ?
Moiz
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).Sel ected Then
Message.Text &= checkboxlist1.Items(i).Tex t & "<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="Ch eck_Clicke d"
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
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).Sel
Message.Text &= checkboxlist1.Items(i).Tex
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="Ch
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ssql = "Select * from testpurpose"
With cmd
.CommandText = Ssql
.Connection = myConnection
End With
myConnection.Open()
dr = cmd.ExecuteReader(CommandB ehavior.Cl oseConnect ion)
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
With cmd
.CommandText = Ssql
.Connection = myConnection
End With
myConnection.Open()
dr = cmd.ExecuteReader(CommandB
While dr.Read
CheckBoxList1.Items.Add(dr
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
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
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
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"
' Bind it to the cblContinents CheckBoxList control.
cblContinents.DataSource = continents
cblContinents.DataTextFiel
cblContinents.DataValueFie
' 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_SelectedInde
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