Link to home
Start Free TrialLog in
Avatar of fc_curling
fc_curlingFlag for Denmark

asked on

How do I get my object back from an item in a DropDownList ?

I have a dictionary with a class (UserClass) asvalue and a int as key
I populate the DropDownList with the dictionary using datasource
When the user selects an item from the CreatorFilter Dropdownlist I want to get the UserClass object back to get access to its ID property

Tried this: Guid UserID = (UserClass)CreatorFilter.SelectedItem).ID;
But gets the following error: Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'UserClass'

How can I access the UserClass object from the selected item in the dropdownlost?
// I have a dictionary with a class (UserClass) asvalue and a int as key
private Dictionary<int, UserClass> createdBy;
 
//I populate the DropDownList with the dictionary using datasource
CreatorFilter.DataSource = createdBy;
CreatorFilter.DataTextField = "Value";
CreatorFilter.DataValueField = "Key";
CreatorFilter.DataBind();
 
//When the user selects an item from the CreatorFilter Dropdownlist I want to get the UserClass object back to get access to its ID property
// Tried this:
 
Guid UserID = (UserClass)CreatorFilter.SelectedItem).ID;
 
// But I get an error: 
// Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'UserClass'
 
// How can I access the UserClass object from the selected item in the dropdownlost?

Open in new window

Avatar of Ashutosh Vyas
Ashutosh Vyas
Flag of India image

I am not too good with Generics, and have not tried your code, but my first look and I feel it should rather be:



Guid UserID = (UserClass)CreatorFilter.SelectedValue).ID;

Open in new window

Try using
(UserClass)CreatorFilter.SelectedValue;;
what type of data you will get for CreatorFilter.SelectedItem?
Avatar of fc_curling

ASKER

@ashutosh9910:, @TechTiger007:

>>Guid creator = ((UserClass)CreatorFilter.SelectedValue).ID;
Gives the following error:  Cannot convert type 'string' to 'UserClass'

@SameerJagdale:
I want to get the UserClass object back which I put into the dropdownlist using the datasource = dictionary.

This is the UserClass:
public class UserClass
{
  public Guid ID;
  public string Username;
  public string Fullname;
  public int? Role;
 
  public UserClass(User user)
  {
    this.ID = user.ID;
    this.Username = user.Username;
    this.Fullname = user.FullName;
    this.Role = user.Role;
  }
  public UserClass()
  {
  }
 
  public override string ToString()
  {
    return this.Username;
  }
 
}

Open in new window

i know that.. what i mean to ask is - currently what is the type of data you are getting..
object o = CreatorFilter.SelectedItem;
need info about o?
CreatorFilter.SelectedItem; returns a ListItem object
Important info: This is a dropdownlist control from a ASP.NET 3.5 webpage.
ASKER CERTIFIED SOLUTION
Avatar of SameerJagdale
SameerJagdale
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the long time to close this question. I implemented a solution based on your code and forgot to close it.