Solved

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

Posted on 2008-10-08
10
202 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:fc_curling
10 Comments
 
LVL 8

Expert Comment

by:Ashutosh Vyas
ID: 22668521
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

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22668523
Try using
(UserClass)CreatorFilter.SelectedValue;;
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22668530
what type of data you will get for CreatorFilter.SelectedItem?
0
 

Author Comment

by:fc_curling
ID: 22668654
@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.

0
 

Author Comment

by:fc_curling
ID: 22668679
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22668733
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?
0
 

Author Comment

by:fc_curling
ID: 22668853
CreatorFilter.SelectedItem; returns a ListItem object
0
 

Author Comment

by:fc_curling
ID: 22669195
Important info: This is a dropdownlist control from a ASP.NET 3.5 webpage.
0
 
LVL 13

Accepted Solution

by:
SameerJagdale earned 500 total points
ID: 22669316

codebehind..selectedindexchanged event

---

        int key = int.Parse(CreatorFilter.SelectedValue);

        UserClass ucclass = createdBy[key];
 

-- page load

createdBy = new Dictionary<int, UserClass>(2);

        createdBy[0] = new UserClass(1);

        createdBy[1] = new UserClass(2);
 

        //I populate the DropDownList with the dictionary using datasource

        CreatorFilter.DataSource = createdBy;

        CreatorFilter.DataTextField = "Value";

        CreatorFilter.DataValueField = "Key";

        CreatorFilter.DataBind();
 
 

my user class

-------

public class UserClass

{

    public UserClass()

    {

        

    }
 

    public UserClass(int id)

    {

        this.Id = id;

    }
 

    private int _id;
 

    public int Id

    {

        get { return _id; }

        set { _id = value; }

    }
 

}

Open in new window

0
 

Author Closing Comment

by:fc_curling
ID: 31504194
Sorry for the long time to close this question. I implemented a solution based on your code and forgot to close it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now