使用类属性填充下拉列表 [英] Fill dropdownlist with class properties

查看:58
本文介绍了使用类属性填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我有一个关于用类的属性填充下拉列表的问题。



这是我的班级:



  public  < span class =code-keyword> class  Person 
{
public string FirstName { get ; set ; }
public string LasttName { get ; set ; }
public 职业职业{ get ; set ; }
}

public class 职业
{
public bool 学生{获取; set ; }
public bool 员工{ get ; set ; }
public bool 失业{ get ; set ; }
}





我有一个页面可以插入或更新一个人,但现在我必须实现选择之间的功能给定的机会。这样您就可以点击Student,Employee或Unemployed,并且状态选择的机会将更改为true。我认为下拉列表是一种选择吗?有这3个选项。还是3个radiobuttons?只允许一个选择。因此,三个中的一个需要改为真,其他人保持虚假。



有谁可以帮我解决这个问题? : - )

解决方案

您没有使用正确的结构满足您的需求以下是适合您的更好的方法:



  public   enum 职业
{
学生,
员工,
失业
}





每当你有谨慎的设置有限的选项最好与enum一起使用。


我认为Nathans的解决方案是可行的。你只需将enum int值保存到你的数据库。



但是如果你想坚持你的类结构有一种方法。



typeof(Occupation).GetProperties()将为您提供一个PropertyInfo类数组。

您可以获取遍历该数组的属性名称。你可能想要的属性是Name。



你想用ListItem填充RadioButtonList:

列表与LT;列表项> occupList =  new 列表< listitem>(); 
foreach (PropertyInfo p in typeof (Occupation).GetProperties())
{
occupList.Add( new ListItem {Text = p.Name,Value = p。名称});
}
occupationList.DataSource = occupationList;
OccupList.DataBind();
< / listitem > < / listitem >


Hi there!

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

This is my Class:

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? :-)

解决方案

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

public enum Occupation
{
    Student,
    Employee,
    Unemployed
}



Whenever you have a discreet set of limited options it's best to go with an enum.


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:

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>


这篇关于使用类属性填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆