ListItemCollection Value in DropDownList - MIA!

  • Thread starter Thread starter Kurt Mang
  • Start date Start date
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
 
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.
 
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
 
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.
 
Back
Top