I haven't had much experience with this....especially with regards to ASP.NET
I gave it a quick test to make sure that I was making sense and it appears that my first response is incorrect. (I came to this conclusion once before when doing something similar...except it was a desktop application....I don't know why I thought this)
The items actually do show up but only after you've done your DataBind() on the ListBox (or CheckBoxList).
Quick example:
Here's the code I have for some Business Object that I created:
-
Public Class BusObj
-
Private _sourceOfData As DataSet
-
-
Public Sub New()
-
Dim someTable As New DataTable
-
someTable.Columns.Add("FirstName")
-
someTable.Columns.Add("LastName")
-
someTable.Columns.Add("HomePhone")
-
-
For i As Integer = 0 To 5
-
Dim dr As DataRow
-
dr = someTable.NewRow
-
dr("FirstName") = "FirstName" + i.ToString
-
dr("LastName") = "LastName" + i.ToString
-
dr("HomePhone") = i.ToString + i.ToString + i.ToString + "-" + i.ToString + i.ToString + i.ToString + i.ToString
-
someTable.Rows.Add(dr)
-
Next
-
_sourceOfData = New DataSet
-
_sourceOfData.Tables.Add(someTable)
-
End Sub
-
Public Function GetPeople() As DataSet
-
Return _sourceOfData
-
End Function
-
End Class
This business object can be used with the ObjectDataSource control so that the CheckBoxList can be bound to it.
Here is the ASP markup:
-
<asp:CheckBoxList ID="objDataSourceTest" runat="server" DataTextField="FirstName" DataValueField="LastName" >
-
</asp:CheckBoxList>
-
-
<asp:ObjectDataSource ID="theSource" runat="server" SelectMethod="GetPeople" TypeName="MyNamespace.BusObj" >
-
</asp:ObjectDataSource>
-
Note that I have declaratively set the DataValueField and DataTextField properties for the CheckBoxList . These indicate which files to bind onto...
So this CheckBoxList will display all of the First Names as text and the LastNames are not displayed (they are the values...this would make more sense if you were using RadioButtonList instead of a ChecKBoxList because the CheckBoxList doesn't make use of the values). If you wanted to change this so that it displayed the "FirstName LastName" as text you'd add a property to the business object that exposes the FullName and set that as the DataTextField.
In the VB code (code behind) I set the CheckBoxList's DataSource to the "theSource" ObjectDataSource and call the DataBind method (in the page load). For some reason you cannot declaratively set the DataSource for a CheckBoxList (I got an exception indicating that this wasn't supported when I tried...I'm probably using the wrong property but don't feel like looking it up right now).
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
objDataSourceTest.DataSource = theSource
-
objDataSourceTest.DataBind()
-
End Sub
After I call the DataBind method the Items are available....so now I can loop through the items and select them:
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
objDataSourceTest.DataSource = theSource
-
objDataSourceTest.DataBind()
-
-
For Each thing As ListItem In objDataSourceTest.Items
-
thing.Selected = True
-
Next
-
End Sub