ListControl SelectedItem gets the lowest index with same value pro

J

Jay

I would like input on a solution for this problem.

Using a ListControl such as RadioButtonList or DropDownlist, I set the
DataSource to something like an ArrayList or DictionaryEntry array of product
Item objects.

public class Item
{
private string _name;
public string Name
{
get {return _name;}
set {_name = value;}
}

private decimal _price;
public decimal Price
{
get {return _price;}
set {_price = value;}
}

public Item(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
}

Item item1 = new Item("Baseball", 1.00m);
Item item2 = new Item("Glove", 1.00m);
Item item3 = new Item("Bat", 1.00m);

ArrayList items = new ArrayList();
items.Add(item1);
items.Add(item2);
items.Add(item3);

rblItems.DataSource = items;
rblItems.DataTextField = "Name";
rblItems.DataValueField = "Price";
rblItems.DataBind();

When a selection is made on the ListControl, SelectedItem always gets the
lowest index item with the same value. According to the docs, this appears to
be by design:

"If the list control allows multiple selections, use this property to get
the properties of the lowest indexed item selected from the list control."

Since all the items have the same Price, selecting a Bat will return that a
Baseball was selected.

I may be missing something simple, but it seems to me that this is
problematic. Obviously, the Name/Price pairs are distinct, but this is lost
when data bound to the control.

Is the only way to overcome this to roll my own control or do something else
kludgey like creating a function to concatenate the Name/Price pair to set as
the ListControl's DataValueField and unravel it after selection?

Thanks for your input.

-- Jay
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top