Solved

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

Posted on 2008-10-08
10
201 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
Comment Utility
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
Comment Utility
Try using
(UserClass)CreatorFilter.SelectedValue;;
0
 
LVL 13

Expert Comment

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

Author Comment

by:fc_curling
Comment Utility
@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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Expert Comment

by:SameerJagdale
Comment Utility
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
Comment Utility
CreatorFilter.SelectedItem; returns a ListItem object
0
 

Author Comment

by:fc_curling
Comment Utility
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
Comment Utility

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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

18 Experts available now in Live!

Get 1:1 Help Now