June 16th, 2009In ASP.NET, How To Create a DropDownList from an ENUM
So, you have an enum defined as follows:
public enum CompanyAddressType { Unknown = 0, Primary = 1, Warehouse = 2, Distribution_Center = 3, Cross_Dock = 4 }
You want to iterate through the list and put the data into an asp.net dropdownlist.
Here is the simple code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string[] names = Enum.GetNames(typeof(CompanyAddressType)); var values = (CompanyAddressType[])Enum.GetValues(typeof(CompanyAddressType)); for (int i = 0; i < names.Length; i++) { DropDownListCompanyAddressType.Items.Add(new ListItem(names[i], values.ToString())); } } }
There are probably easier ways to do it, but this works.
and…
HTH’s.









June 16th, 2009 at 10:35 pm
[...] In ASP.NET, How To Create a DropDownList from an ENUM … [...]
June 16th, 2009 at 11:31 pm
I thought you didn’t do ASP.NET anymore?? When/why do you need ASP.NET form controls?
June 17th, 2009 at 5:37 am
I still do ASP.NET for building fast interfaces. There is still no way I can crank out ExtJS quickly