binding - DropDownList flaw/bug?

M

Matthew.DelVecchio

hey gang,

ok so i experienced something that doesnt operate as i would have
expected. in a component i build up a ListItemCollection in function
..GetGalleries() like so:


Dim galleries As New System.Web.UI.WebControls.ListItemCollection

'loop & add to collection
Dim node As XmlNode
For Each node In galleryNodes
galleries.Add(New ListItem("sometext", "somevalue"))
Next

Return galleries

then, in my application i attempt to bind like so:

Dim galleries As ListItemCollection = im.GetGalleries(year)

ddlGallery.DataSource = galleries
ddlGallery.DataBind()

....now, you would think then, that the ddlGallery dropdownlist would
take the returned ListItemCollection and bind its list items to the
list. It does -- sort of.. it does, BUT it doesnt use the ".value"
porton of the returned ListItems ("somevalue"). instead, it just takes
the ".text" value of each ListItem ("sometext") and sticks that in both
the text AND the value of each item in ddlGallery. whaaaa?

clearly, the expected behavior would be for ddlGallery list control to
see that its getting a ListItemCollection as a datasource, and
intelligently bind it -- by using the existing .text and .value values
if of the collection's ListItems... after all, this isnt any ole
collection, it's a *ListItemCollection*...!

as a work around, i have to loop thru the returned collection and add
each individually, like so:

Dim galleries As ListItemCollection = im.GetGalleriesForYear(year)

Dim item As ListItem
For Each item In galleries
ddlGallery.Items.Add(item)
Next



thats pretty lame. can anyone comment on this?

thanks!
matt
 
H

Harolds

try this
ddlGallery.DataSource = galleries
ddlGallery.DataTextField = "Text"
ddlGallery.DataValueField = "Value"
ddlGallery.DataBind()
 
M

Matthew.DelVecchio

ah... yes im familar with those properties, however i thought they only
pertained to the string name of a data column, say from a dataset or
datatable -- i didnt realize they would accept "Text" and "Value"..!
that is cool and saves me code. thanks.

case closed!

now, does that work only because the ListItemCollection uses "Text" and
"Value" as internal names? meaning, can this work with say a
multi-dimensional array, or dictionary object, etc -- other collection
objects of 2-part items? probably not, because those collection types
dont have "Text" or "Value" -- in which case, how do we designate...

what im really getting at, is an XML nodelist... say if i bind a
nodelist to a DropDownList, im not aware of a way -- even using
..DataTextFile & .DataValueField -- to designate which peices of data it
should use.

for ex:

<people>
<person id="1" name="Joe">[blah]</person>
<person id="2" name="Bob">[blah]</person>
<person id="3" name="Roberto">[blah]</person>
</people>

....you can successfully use this nodelist as a .DataSource, but i
havent figured out a way to specificy which elements of the data to use
for what.

thanks,
matt
 

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,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top