Asp. NET, binding enum type

Source: Internet
Author: User
ASP.net has been used to enumerate many places in project development, and to display the enumeration to the Drop-down list box Our usual method is to add one to the Drop-down list control. It's okay to do this, but if the enumeration value in my enumeration type changes, then you have to look around the way and then change it.

Here I have the reflection dynamic binding enumeration value can be easily modified, the code is as follows:

public enum Eume
{
Monday
Tuesday
Wednesday
Thursday
Friday
}
Binding method:

private void Binderdays ()
{
Type type = typeof (Eume);
Fieldinfo[] fields= type. GetFields ();
int count = fields. Length;
for (int i = 1; i < count; i++)
{
FieldInfo field = Fields[i];
ListItem item=new ListItem (field. Name);
Days. Items.Add (item);
}
}
Ok that's fine, but unfortunately the enumeration value shows that the conversion does not support string types, and it would be nice if supported.

Eume em = (Eume) days. SelectedValue; Such a realistic and inaccurate
Eume em = (Eume) days. selectedindex;//that's the right thing to write.
A little bit of tips to share with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.