I seem to be having a lot of trouble using the CheckBoxList Control. I have created two pages to try and find out what I have been doing wrong. Both pages are causing me problems but different problems.
The page that I want to work will not display the selected items after clicking the button.
The page I created to try and locate the problem will display a duplicate set of items when loaded but displays the selected items. If I remove a some code from the Sub line it will not display to duplicate info. See code below.
Team.aspx.vb (Page I want to work)
Code Snippets
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Team Details
'Skipped cookie Stuff
'Skipped DB Stuff
'Request for Join
strSQL = "Select tblMemberID from tblRequest where tblTeamID = " & TeamID.Rows(0)(0).ToString
Dim Reqs As New DataTable
Dim CheckReqs As New System.Data.OleDb.OleDbDataAdapter(strSQL, Conn)
CheckReqs.Fill(Reqs)
Dim i As Integer
For i = 0 To Reqs.Rows.Count - 1
strSQL = "Select UserName, FName, LName from tblMembers where tblMemberID = " & Reqs.Rows(i)(0)
Dim UserReq As New DataTable
Dim CheckUserReqs As New System.Data.OleDb.OleDbDataAdapter(strSQL, Conn)
CheckUserReqs.Fill(UserReq)
If UserReq.Rows.Count <> 0 Then
Dim lItem As New ListItem
lItem.Text = UserReq.Rows(0)(0) & "/" & UserReq.Rows(0)(1) & " " & UserReq.Rows(0)(2)
lItem.Value = Reqs.Rows(i)(0)
cbList.Items.Add(lItem)
End If
Next
End If
End Sub
Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
Dim i As Integer
For i = 0 To cbList.Items.Count - 1
If cbList.Items(i).Selected Then
lblMessage.Text &= cbList.Items(i).Text
End If
Next
End Sub
WebForm1.aspx.vb
Complete Code
'When "Handles MyBase.Load" is removed from the below line Duplicates do not show up
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 IsPostBack Then
Dim i As Integer
i = 1
checkboxlist1.Items.Clear()
For i = 1 To 6
Dim lItem As New ListItem
lItem.Text = "Item " & i
lItem.Value = i
checkboxlist1.Items.Add(lItem)
lItem = Nothing
Next
'End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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
If this was a double post please accept my apologies. Wasn't sure if it went through the first time.
--------------------------------
From: Michael Manuel
-----------------------
Posted by a user from .NET 247 (
http://www.dotnet247.com/)
<Id>BoQbfV0eskKVCUISavFzdQ==</Id>