c# asp.net 中DropDownList控件绑定枚举数据

1.枚举(enum)代码:

private enum heros
   {
        德玛 = 0,
        皇子 = 10,
        大头 = 20,
        剑圣 = 30,
    }

如果不为枚举数列表中的元素指定值,则它们的值将自动递增,从1开始。

 

2.cs代码:

 private void LoadData()
{
     //检索枚举heros返回包含每个成员的值的数组
     Array herosArray = Enum.GetValues(typeof(heros));

     foreach (int value in herosArray)
     {
          ListItem item = new ListItem();
          item.Text = Enum.GetName(typeof(heros), value);
          item.Value = value.ToString();
          ddrHeros.Items.Add(item);
     }
}

3.结果:

c#中DropDownList控件绑定枚举数据

源码:

c#中DropDownList控件绑定枚举数据

 

相关文章:

  • 2022-12-23
  • 2021-07-15
  • 2022-02-06
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2021-06-21
  • 2022-02-06
  • 2021-08-30
  • 2022-12-23
  • 2021-08-14
  • 2021-06-26
  • 2022-12-23
相关资源
相似解决方案