下拉列表框绑定枚举类型值

将枚举类型作为数据源绑定在一个DROPDOWNLIST上,并指定用户可见枚举的真实值还是名称

    后台绑定枚举类型

 public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetListType();	
        }

        private void GetListType()
        {
            DropDownListMenu.Items.Clear();
            DropDownListMenu.DataSource = ListTypeForEnum();
            DropDownListMenu.DataValueField = "value";
            DropDownListMenu.DataTextField = "text";
            DropDownListMenu.DataBind();
        }
        public static IList ListTypeForEnum()
        {
            ArrayList list = new ArrayList();
            foreach (int i in Enum.GetValues(typeof(EnumActivityType)))
            {
                ListItem listitem = new ListItem(Enum.GetName(typeof(EnumActivityType), i), i.ToString());
                list.Add(listitem);
            }
            return list;
        }
        public enum EnumActivityType
        {
            Task = 344,
            Fax = 345,
            Phone = 346,
            Email = 347,
            Reminder = 348,
            Appointment = 349,
            Expiration = 350,
            Inquiry = 367
        }
    }

 生成后的网页代码

 <select name="DropDownListMenu" id="DropDownListMenu">
	<option value="344">Task</option>
	<option value="345">Fax</option>
	<option value="346">Phone</option>
	<option value="347">Email</option>
	<option value="348">Reminder</option>
	<option value="349">Appointment</option>
	<option value="350">Expiration</option>
	<option value="367">Inquiry</option>

</select>

 

你可能感兴趣的:(下拉框绑定枚举值)