get the value in ListItem

4.3k views Asked by At

I´m trying to get the value of from all ListItems in a DropDown list, but when I treat them as a Control, I get an error:

cannot convert type 'system.web.ui.control' to 'system.web.ui.webcontrols.listitem'

My code:

if (c.GetType() == typeof(ListItem))
 string id = c.ID;
 string value = ((ListItem)(c)).Value; ///I get the error here (ListItem)(c)

How can I get those values? I would like to do it without knowing the ID from those elements.

1

There are 1 answers

4
Mert On BEST ANSWER

Can you try this?

foreach (var item in ctl.Items) 
{
    var value = item.Value;
}