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

后端 未结 25 2337
既然无缘
既然无缘 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:36

    For those of us that want a working C# solution that works with any drop and enum...

    private void LoadConsciousnessDrop()
    {
        string sel_val = this.drp_Consciousness.SelectedValue;
        this.drp_Consciousness.Items.Clear();
        string[] names = Enum.GetNames(typeof(Consciousness));
        
        for (int i = 0; i < names.Length; i++)
            this.drp_Consciousness.Items.Add(new ListItem(names[i], ((int)((Consciousness)Enum.Parse(typeof(Consciousness), names[i]))).ToString()));
    
        this.drp_Consciousness.SelectedValue = String.IsNullOrWhiteSpace(sel_val) ? null : sel_val;
    }
    
    0 讨论(0)
提交回复
热议问题