ListItemCollection Value in DropDownList - MIA!

K

Kurt Mang

Hi all --

I hate to yell "BUG", but can anyone explain why I cannot
get a dropdown list / checkboxlist
to recognise the Value property of a ListItemCollection
(created at runtime) that I bind to it?

This is a simplified version of what I have. I see no
reason why this shouldn't work:

' Create the listitemcollection property:
Private ReadOnly Property ListMonths() As
ListItemCollection
Get
If Me._licMonths Is Nothing Then
Dim arrTemp As New
ListItemCollection
With arrTemp
.Add(New ListItem("Select
a month:", "00"))
.Add(New ListItem
("January", "01"))
.Add(New ListItem
("February", "02"))
.Add(New ListItem
("March", "03"))
.Add(New ListItem
("April", "04"))
.Add(New ListItem
("May", "05"))
.Add(New ListItem
("June", "06"))
.Add(New ListItem
("July", "07"))
.Add(New ListItem
("August", "08"))
.Add(New ListItem
("September", "09"))
.Add(New ListItem
("October", "10"))
.Add(New ListItem
("November", "11"))
.Add(New ListItem
("December", "12"))
End With
Me._licMonths = arrTemp
End If
Return Me._licMonths
End Get
End Property

' bind to a dropdown list: called in Page_Load
private sub PopulateDropDowns()
If Me.ddlDest1Month.Items.Count = 0 Then
With Me.ddlDest1Month
.DataSource = Me.ListMonths
.DataBind()
End With
End If
end sub


' the result (from view source of aspx page):
<select name="ucPreferences:ddlDest1Month"
id="ucPreferences_ddlDest1Month">
<option selected="selected" value="Select a
month:">Select a month:</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>

</select>

Aargh! Why aren't my values being sent to the control?

Thanks for any help / insight / workarounds. I don't
want to resort to checking the Text property rather than
the Value property.

Kurt Mang
Vancouver, BC
 
M

Marina

I don't think you can databind to a listitem collection.

You would have to set the Items property to the collection you are
returning. If this property is not readonly, then you can go ahead and do
that.

Otherwise, with the current set up, you would have to loop through the
connection, and readd all the items to the actual dropdown.
 
K

Kurt Mang

OK, unbelievably this is what you have to do to get the
ListItemCollection to bind CORRECTLY to your data-bound
web control, assuming you want to include seperate Text &
Value properties:

With Me.ddlDest1Month
.DataSource = Me.ListMonths ' a ListItemCollection
.DataValueField = "Value"
.DataTextField = "Text"
.DataBind()
End With

.... there you have it. The control - be it a dropdown
list, checkbox list, etc. - doesn't recognise that the
default Value / Text properties shold be the
ListItemCollection's Value / Text properties. I LOVE
VB.NET, but I gotta say that's pretty dumb.

Kurt Mang
www.skyrocketsolutions.com
 
S

szabelin

It's not dumb - you can bind most controls to any
datasource by setting its DataSource property. The only
requirement is that DataSource be set to an object that
implements certain interface that allows enumeration, and
it doesn't care about specific type it's bound to. The
control needs to know which columns to read from, in your
case they are public properties of ListItem: "Value"
and "Text". If ListItem had a property called "Color" you
could bind to that as well. Makes sence now? My 2 cents
worth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top