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++;
}