473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DropDownList Bound to ArrayList of Custom Objects

I've got a dropdownlist (webform) bound to an arraylist which holds my
several of a custom object as follows:

fldOption.DataSource = prod.Items
fldOption.DataTextField = "Options"
fldOption.DataValueField = "ID"
fldOption.DataBind()

(prod.Items is an arraylist containing objects of type cItem)

Seems straight forward enough, and the control works fine.

The problem comes when I try to get a reference to the cItem that is
selected. One would think that:

dim i as cItem = CType(fldOption.SelectedItem, cItem)

would do the trick...but it doesn't. Instead I get:

Value of type 'System.Web.UI.WebControls.ListItem' cannot be converted
to 'cItem'.

Yes, I could simply loop through the prod.Items arraylist and look for
matching IDs...in fact I'll probably go code it that way now, since I
need to get this done. But that seems to be a less than elegant
solution to the problem.

Am I misunderstanding something here? Any help appreciated.

Nov 19 '05 #1
3 1378
SelectedItem is the Item in the list. It has Text and Value properties. That's
what you want to access.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I've got a dropdownlist (webform) bound to an arraylist which holds my
several of a custom object as follows:

fldOption.DataSource = prod.Items
fldOption.DataTextField = "Options"
fldOption.DataValueField = "ID"
fldOption.DataBind()
(prod.Items is an arraylist containing objects of type cItem)

Seems straight forward enough, and the control works fine.

The problem comes when I try to get a reference to the cItem that is
selected. One would think that:

dim i as cItem = CType(fldOption.SelectedItem, cItem)

would do the trick...but it doesn't. Instead I get:

Value of type 'System.Web.UI.WebControls.ListItem' cannot be converted
to 'cItem'.

Yes, I could simply loop through the prod.Items arraylist and look for
matching IDs...in fact I'll probably go code it that way now, since I
need to get this done. But that seems to be a less than elegant
solution to the problem.

Am I misunderstanding something here? Any help appreciated.


Nov 19 '05 #2
Thank you for your reply.

I understand the Text and Value properties, but I need access to all
the properties and methods on the cItem object, so they don't help me
here.

Alternately, I'm wondering if the items in the dropdownlist are always
in the same order as the items in the arraylist. If that's the case, I
could simply refer to prod.items(fldOption.SelectedIndex), which would
certainly be cleaner than doing a for...each through the arraylist,
which can be fairly large.

Nov 19 '05 #3
> Alternately, I'm wondering if the items in the dropdownlist are always
in the same order as the items in the arraylist. If that's the case,
I could simply refer to prod.items(fldOption.SelectedIndex), which
would certainly be cleaner than doing a for...each through the
arraylist, which can be fairly large.


Yes that would probabaly work. I'd tend to be more defensive and find the
cItem entry in the collection based upon the ID. You'd want to use Array.BinarySearch
to do that.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Omer | last post by:
Hi All, We are trying to invoke EJB methods on simple stateless session beans from an Applet running remotely. One of our methods needs to return an ArrayList of Serializable objects. We...
0
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
0
by: John | last post by:
Hi, I have a dropdownlist with x amount of list items, however quite often a lot of the list items have the same value. The dropdownlist is bound to an ArrayList of objects and I'm using the...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
4
by: JL | last post by:
I am used a hashtable for dropdownlist data source but it seems un-sorted on output. Anybody who know the sort method in dropdownlist box? thanks a lot
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.