使用反射用枚举填充下拉列表
我正在使用反射填充一个带有读取类属性的控件的页面。如果属性类型是'String'
,我将添加一个文本框。如果属性类型是枚举,我将添加一个下拉列表。现在我必须用枚举填充下拉选项。这怎么能做到呢?
我使用控件填充页面的枚举定义类(Assignment
)和类(classOne
)都位于相同的命名空间(MySolution.Data)中
。当属性名称为“SkillLevel”时循环访问 classOne 属性时,我必须转到分配类,获取枚举 SkillLevelEnum
的成员并填充下拉列表。
其他下拉菜单也需要做同样的事情。
我的代码:
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
}
}
谢谢
编辑:
我不想对任何属性名称进行硬编码。我正在循环访问以下属性。
属性 = Utility.GetAllPropertyForClass("className")
面板面板 = new Panel();
panelMe.Controls.Add(面板);
foreach(属性中的PropertyInfo属性) {
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++;
}
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++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是如何将枚举与下拉列表绑定的示例
Here are sample examples for how to bind enum with dropdown list
尝试
enumvalues 将是一个数组,而 enumNames 是一个字符串数组。
Try
enumvalues will be an array and enumNames is a string array.
如果您的枚举是 MinimalSkillsEnum,则这应该有效:
If your enum is MinimalSkillsEnum, this should work:
您可以通过 GetNames 函数访问枚举的标签,
然后您可以直接将其用作下拉列表的数据源
You can access the labels of an Enum via the GetNames function
You could then use that directly as the data source for your drop-down
将枚举绑定到下拉列表检查 如何将 Enum 绑定到 ASP.NET 中的 DropDownList 控件?
to bind enum to dropdown check How do you bind an Enum to a DropDownList control in ASP.NET?