Problem with getting selected OBJECT from dropdownlist

asp.netdrop-down-menu

I have a problem with getting selected objectfrom my list.

I bind collection of users to dropdownlist by:

 ddlContractors.DataSource = service.GetAllUsers();
                ddlContractors.DataTextField = "Name";
                ddlContractors.DataValueField = "Id";
                ddlContractors.DataBind();

It's working.
But when I try getting selected object by:

var user = (User)ddlContractors.SelectedItem;

I get:

    (User)ddlContractors.SelectedItem   Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'GWDSite.GWDService.User'

How can I get object user from dropdownlist ? I can change type of my list control if it is necessary

Best Answer

The value field in the dropdown list is the field "Id" not the User object. so 'SelectedItem' is returning the "Id" value -- not the object. You can use that Id to lookup the User object (from session or cache or wherever you can keep it)