Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!

I've got a question about filling a dropdownlist with the properties of a class.

This is my Class:

C#
public class Person
   {
       public string FirstName { get; set; }
       public string LasttName { get; set; }
       public Occupation Occupation { get; set; }
   }

public class Occupation 
    {
        public bool Student{ get; set; }
        public bool Employee{ get; set; }
        public bool Unemployed{ get; set; }
    }


I have a page to insert or update a person but now I have to implement the functionality to choose between the given opportunities. So that you click on Student, Employee or Unemployed and that the state chosen opportunity will be changed to true. I think a dropdownlist is an option? With those 3 options. Or 3 radiobuttons? Only one choose is allowed. So one of the three needs to change to true, to other stay false.

Anybody who can help me out on this? :-)
Posted

You're not using the correct structure for your needs Here's what would work better for you:

C#
public enum Occupation
{
    Student,
    Employee,
    Unemployed
}


Whenever you have a discreet set of limited options it's best to go with an enum.
 
Share this answer
 
Comments
Cas Dijkstra 4-Feb-15 7:15am    
And can I save that to the database? Where I have those 3 options as a bit? So if student is chosen, that student will be set to true for the selected person in the database.
Nathan Minier 4-Feb-15 7:22am    
That depends largely on how you're communicating with the database, but you can always cast an enum to int and back again as needed.

If you want to make things a little easier on that front, you can even set your enum up with explicit numeric representations:

public enum Example
{
None = 0,
Something = 1,
SomethingElse = 2
}

This is also how you setup enum flags (where multiple bits can be flipped at once).

Edit: I know that Entity Framework does this translation automatically. I'm not 100% sure about other data access schemes.
I think Nathans solution is the way to go. You just save the enum int value to your database.

But if you want to stick to your class structure there is a way.

typeof(Occupation).GetProperties() will give you an array of PropertyInfo class.
you can get the names of your properties iterating through that array. the property you probably want is Name.

say you want to populate a RadioButtonList with ListItems:
C#
List<listitem> occupationList = new List<listitem>();
foreach (PropertyInfo p in typeof(Occupation).GetProperties())
{
   occupationList.Add(new ListItem {Text=p.Name, Value=p.name});
}
occupationList.DataSource = occupationList;
occupationList.DataBind();
</listitem></listitem>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900