i think you can
lst = (ListItemCollection)Campus
lstCampus.DataSource=lst;
lstCampus.DataBind();
Main Topics
Browse All TopicsI am binding a listitemcollection to a listbox control. The listitemcollection is returned from a function inside a class. The probelm is when the control is bound the value of the text property of the listitem in the collection is placed in the value property.
CodeBehind
lstCampus.DataSource = Campuses.GetCampuses(Sessi
lstCampus.DataBind()
Class
Public Overloads Function GetCampuses(ByVal DistrictID As String, ByVal conn As SqlConnection) As ListItemCollection
Dim lst As New ListItemCollection
Dim cmd As New SqlCommand("Campus_Select_
cmd.CommandType = CommandType.StoredProcedur
Dim prm As New SqlParameter
prm = cmd.Parameters.Add("@prmDi
prm.Value = DistrictID
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
lst.Add(New ListItem(reader("CampusNam
End While
reader.Close()
cmd.Dispose()
Return lst
End Function
HTML Source/Rendered Output:
<option selected="selected" value="Sample High School">Sample High School</option>
<option value="Sample Intermediate School">Sample Intermediate School</option>
<option value="Sample Middle School">Sample Middle School</option>
Should Be:
<option selected="selected" value="12345041">Sample High School</option>
<option value="123456001">Sample Intermediate School</option>
<option value="123456203">Sample Middle School</option>
Any ideas?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
No it didnt give an error it just provided same result as what I explained in my intial post.
Tried this with also the same result as initial post"
lstCampus.DataSource = CType(Campuses.GetCampuses
lstCampus.DataBind()
AND
lstCampus.DataSource = DirectCast(Campuses.GetCam
lstCampus.DataBind()
Just to be sure the items in the returned listitemcollection were not the problem I add this:
Dim lst As New ListItemCollection
lst = Campuses.GetCampuses(Sessi
For Each item As ListItem In lst
Response.Write("Text: " & item.Text & " Value: " & item.Value & "<BR>")
Next
add this was the result:
Text: Sample High School Value: 123456002
Text: Sample Intermediate School Value: 123456100
Text: Sample Middle School Value: 123456041
So I know the items in the returned collection are correct.
Yeah I tried that earlierlstCampus.DataTextF
lstCampus.DataTextField = "CampusID"
lstCampus.DataSource = Campuses.GetCampuses(Sessi
lstCampus.DataBind()
and got the following error:
DataBinder.Eval: 'System.Web.UI.WebControls
Here is Microsoft's example of binding a listitemcollection to litbox:
Dim listBoxData As New ListItemCollection()
listBoxData.Add(New ListItem("apples"))
listBoxData.Add(New ListItem("bananas"))
listBoxData.Add(New ListItem("cherries"))
listBoxData.Add("grapes")
listBoxData.Add("mangos")
listBoxData.Add("oranges")
ListBox1.DataSource = listBoxData
ListBox1.DataBind()
After seeing that it appears that only the text property can be bound.
km1039:
I was actually experiencing the same issue and was disappointed by the fact that MSDN did not list an example of this (since this is a very common task). I actually realized the solution when I considered some simple data binding. If you think about it, a ListItemCollection is simply a collection of ListItem objects. A ListItem exposes two commonly used members (Text and Value). Considering that, here is your solution:
lstCampus.DataSource = Campuses.GetCampuses(Sessi
lstCampus.DataTextField = "Text"
lstCampus.DataValueField = "Value"
lstCampus.DataBind()
I hope this helps.
Business Accounts
Answer for Membership
by: km1039Posted on 2005-01-11 at 07:43:02ID: 13014722
This works: on("gblDis trictID"), conn)
Dim lst As New ListItemCollection
lst = Campuses.GetCampuses(Sessi
For Each item As ListItem In lst
lstCampus.Items.Add(item)
Next
but I don't understand why I can't bind directly yo the returned listitemcollection