How do you bind an Enum to a DropDownList control in ASP.NET?

后端 未结 25 2588
既然无缘
既然无缘 2020-11-29 15:41

Let\'s say I have the following simple enum:

enum Response
{
    Yes = 1,
    No = 2,
    Maybe = 3
}

How can I bind this enum to a DropDow

25条回答
  •  星月不相逢
    2020-11-29 16:25

    You can do this a lot shorter

    public enum Test
        {
            Test1 = 1,
            Test2 = 2,
            Test3 = 3
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                var items = Enum.GetValues(typeof(Test));
    
                foreach (var item in items)
                {
                    //Gives you the names
                    Console.WriteLine(item);
                }
    
    
                foreach(var item in (Test[])items)
                {
                    // Gives you the numbers
                    Console.WriteLine((int)item);
                }
            }
        }
    

提交回复
热议问题