C# – How to get the selected value in a DropDownList in the Object Type (not string)

asp.netc#-2.0data-binding

I fill up a List<> with an object that has properties. I can bind to the DropDownList without any problem:

     cbo.DataSource = possibleChoice;
     cbo.DataValueField = "Value";
     cbo.DataTextField = "Display";
     cbo.DataBind();

But I can't find a way to get the Value. I know the DropDownList has SelectedValue but it return a String instead of my type of object…

     MyObjectType myObj = (MyObjectType)this.cbo.SelectedValue;//Err! Return a String

How can I get the object from the DropDownList?

Best Answer

I think you should be able to do the following

MyObjectType myObj = (MyObjectType)this.cboTimeArea.SelectedItem.Value;

But if not, the following will work

MyObjectType myObj = possibleChoice[this.cboTimeArea.SelectedIndex];