Solved

loop through an enum and add to dropdownlist

Posted on 2013-10-23
2
309 Views
Last Modified: 2013-10-24
I need to loop through an enum with the values for Gender that need to be added to a dropdownlist. How do I set up my foreach statement for this?

Admin.SiteCentralLookupFactoryLookUpTypes LookUpGenders = Admin.SiteCentralLookupFactoryLookUpTypes.Genders;

Where Admin.SiteCentralLookupFactoryLookUpTypes is an enum

LookupValues = MyFactory.GetLookupItems(LookUpGenders, AssociateID);
where (Local Variable) List<Admin.LookUpObject> LookupValues

[0]
LookUpDescription = "Female"
LookUpDescriptionField = "Female"

LookUpId = "1"
LookUpIdField = "1"

LookUpName = "F"
LookUpNameField = "F"

[1]
LookUpDescription = "Male"
LookUpDescriptionField = "Male"

LookUpId = "2"
LookUpIdField = "2"

LookUpName = "M"
LookUpNameField = "M"

            
//GENDER ID DROPDOWNLIST
LookupValues = MyFactory.GetLookupItems(LookUpGenders, EmployeeID);
foreach (LookupValues eGenders in Enum.GetValues(typeof(LookupValues)))
{
      ddlGenderID.Items.Add(eGenders);        
}

ddlGenderID.SelectedValue = EmployeeDetails.GenderID.ToString();
0
Comment
Question by:RedXavier2k4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 39595133
Try like given below:
enum Gender
{
    Female = 1,
    Male = 2
}


Array itemValues = System.Enum.GetValues(typeof(Gender));
Array itemNames = System.Enum.GetNames(typeof(Gender));

for (int i = 0; i <= itemNames.Length - 1 ; i++) 
{
    ListItem item = new ListItem(itemNames[i], itemValues[i]);
    ddlGenderID.Items.Add(item);
}

Open in new window

0
 

Author Closing Comment

by:RedXavier2k4
ID: 39596825
Thanks That's what I was looking for. I also found a way with using the datasource.

                //GENDER ID DROPDOWNLIST
                LookupValues = MyFactory.GetLookupItems(Admin.SiteCentralLookupFactoryLookUpTypes.Genders, tSiteID);
                ddlGenderID.DataSource = LookupValues;
                ddlGenderID.DataTextField = "LookupName";
                ddlGenderID.DataValueField = "LookupId";
                ddlGenderID.DataBind();
                if (EmployeeDetails.GenderID.ToString().Trim() != "")
                {
                    ddlGenderID.SelectedValue = EmployeeDetails.GenderID.ToString();
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 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