fc_curling
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.S electedIte m).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 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.S
But gets the following error: Cannot convert type 'System.Web.UI.WebControls
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?
Try using
(UserClass)CreatorFilter.S electedVal ue;;
(UserClass)CreatorFilter.S
what type of data you will get for CreatorFilter.SelectedItem ?
ASKER
@ashutosh9910:, @TechTiger007:
>>Guid creator = ((UserClass)CreatorFilter. SelectedVa lue).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.
>>Guid creator = ((UserClass)CreatorFilter.
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.
ASKER
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;
}
}
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?
object o = CreatorFilter.SelectedItem
need info about o?
ASKER
CreatorFilter.SelectedItem ; returns a ListItem object
ASKER
Important info: This is a dropdownlist control from a ASP.NET 3.5 webpage.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the long time to close this question. I implemented a solution based on your code and forgot to close it.
Open in new window