Saturday, March 24, 2012

Populate Dropdownlist with arraylist

I thought that by using the follwing code that the dropdown value wouldbe the second item mentioned in my list item but it seems to be thefirst that get in the selectedTex as well as the selectedValuefield. Is there something that I am missing so that theselectedValue would be the second item of my listeItem?
Thank you
<code>
'********************************************************************************
'********************************************************************************
Private Sub insertTables()
'********************************************************************************
Dim lstColums As New ArrayList
lstColums.Add(New ListItem("Software", "Cl_Systems.Software"))
lstColums.Add(New ListItem("SystemType", "Cl_Systems.Software"))
lstColums.Add(New ListItem("SystemPurchaseDate", "Cl_Systems.SystemPurchaseDate"))
lstColums.Add(New ListItem("ServicePlan", "Cl_Systems.ServicePlan"))
lstColums.Add(New ListItem("ServicePlan_Purchase", "Cl_Systems.ServicePlan"))
lstColums.Add(New ListItem("ServicePlan_WarrentyEnd", "Cl_Systems.WarrentyEnd"))
lstColums.Add(New ListItem("ServicePlan_RenewalDate", "Cl_Systems.RenewalDate"))
lstColums.Add(New ListItem("BuiltID", "Cl_Systems.BuiltID"))
lstColums.Add(New ListItem("Software Version", "Cl_Systems.Version"))
lstColums.Add(New ListItem("Modules", "Cl_Systems.Modules"))
lstColums.Add(New ListItem("Packages", "Cl_Systems.Packages"))
lstColums.Insert(0, "--Select Field--")
ddFields.DataSource = lstColums
ddFields.DataBind()
End Sub
</code>
You added this "lstColums.Insert(0, "--Select Field--")" at the first posion which is "0" and you didn't set your selectedvalue when the DDL loaded, so the default selectedvalue is the first one.

Event if I take that line away it give me the same result.


Are you really creating the list like this? If so, I suggest either (a) creating a ListItemCollection instead, or (b) skipping the intermediate object altogether and just adding the items to the dropdownlist's Items collection.

0 comments:

Post a Comment