3

I am populating a page with controls reading properties of a class using reflection. If the property type is 'String' I will add a text-box. If the property type is enum I am adding a dropdownlist. Now I have to populate the dropdown options with enums. How can this be done?

Both the enum definition class(Assignment) and the class(classOne) using which I am populating the page with controls are in the same Namespace(MySolution.Data). While looping through classOne properties when the property name is 'SkillLevel' I will have to go to assignment class get the members of enum SkillLevelEnum and populate the dropdown.

Same needs to be done for other dropdowns also.

My Code:

namespace MySolution.Data
{
  public class classOne : MyAdapter
    {
        private string _Model;

        public string Model
        {
            get { return _Model; }
            set { _Model = value; }
        }

        private Assignement.SkillLevelEnum _SkillLevel;

        public Assignement.SkillLevelEnum SkillLevel
        {
            get { return _SkillLevel; }
            set { _SkillLevel = value; }
        }

        private Assignement.MinimalSkillsEnum _MinimalSkill;

        public Assignement.MinimalSkillsEnum MinimalSkill
        {
            get { return _MinimalSkill; }
            set { _MinimalSkill = value; }
        }

        public Assignemen.WorkLoadEnum WorkLoad
        {
            get { return _WorkLoad; }
            set { _WorkLoad = value; }
        }
    }

   public class Assignement : MyAdapter
     {

        #region Enumerations

        public enum SkillLevelEnum
        {
            LowerSkills = 0, HighestSkills = 1, Any = 2
        }

        public enum MinimalSkillsEnum
        {
            Accountable = 0,
            Responsible = 1,
            Expert = 2,
            Senior = 3,
            Medium = 4,
            Junior = 5
        }

        public enum WorkLoadEnum
        {
            LessBusy = 0, MostBusy = 1, Any = 2
        }

        #endregion
   }

}

Thanks

Edit:

I don't want to hardcode any of the property names. I am looping through the properties as below.

properties = Utility.GetAllPropertyForClass("className")

Panel panel = new Panel();
panelMe.Controls.Add(panel);

foreach (PropertyInfo property in properties) {

        if (!property.PropertyType.IsEnum)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt" + i.ToString();                
            panel.Controls.Add(txt);  
        }
        else
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i.ToString();

            // Here based on the property.name i need to get the enum members which is defined in a different class using reflection

            panel.Controls.Add(ddl);
        }        

        panel.Controls.Add(new LiteralControl("<br/>"));
        i++;
    }    

5 Answers 5

4

Here are sample examples for how to bind enum with dropdown list

public enum ProgrammingLanguage
{
    CSharp,
    VB,
    JAVA
}
foreach (ProgrammingLanguage enmLnaguage  in Enum.GetValues(typeof(ProgrammingLanguage)))
{
     cboProgrammingLanguage.Items.Add(new ListItem(enmLnaguage.ToString(), Convert.ToInt32( enmLnaguage).ToString()));
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try

var enumvalues=Enum.GetValues(typeof(MinimalSkillsEnum));
var enumNames=Enum.GetNames(typeof(MinimalSkillsEnum));

enumvalues will be an array and enumNames is a string array.

Comments

1

If your enum is MinimalSkillsEnum, this should work:

string[] enumOptions = Enum.GetNames(typeof(MinimalSkillsEnum));

Comments

0

You can access the labels of an Enum via the GetNames function

List<string> options = Enum.GetName(typeof(MyEnum));

You could then use that directly as the data source for your drop-down

MyDropDown.DataSource = options;
MyDropDown.DataBind();

Comments

0

to bind enum to dropdown check How do you bind an Enum to a DropDownList control in ASP.NET?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.