Odpowiedzi:
I prawdopodobnie nie wiążą dane, jak to jest enum, i to nie zmieni się po czasie kompilacji (chyba, że mam jeden z tych Stoopid momentach).
Lepiej po prostu iterować przez wyliczenie:
Dim itemValues As Array = System.Enum.GetValues(GetType(Response))
Dim itemNames As Array = System.Enum.GetNames(GetType(Response))
For i As Integer = 0 To itemNames.Length - 1
Dim item As New ListItem(itemNames(i), itemValues(i))
dropdownlist.Items.Add(item)
Next
Lub to samo w C #
Array itemValues = System.Enum.GetValues(typeof(Response));
Array itemNames = System.Enum.GetNames(typeof(Response));
for (int i = 0; i <= itemNames.Length - 1 ; i++) {
ListItem item = new ListItem(itemNames[i], itemValues[i]);
dropdownlist.Items.Add(item);
}
Użyj następującej klasy narzędziowej, Enumerationaby uzyskać IDictionary<int,string>(para wartość wyliczenia i nazwa) z wyliczenia ; następnie powiąż IDictionary z możliwą do powiązania kontrolką .
public static class Enumeration
{
public static IDictionary<int, string> GetAll<TEnum>() where TEnum: struct
{
var enumerationType = typeof (TEnum);
if (!enumerationType.IsEnum)
throw new ArgumentException("Enumeration type is expected.");
var dictionary = new Dictionary<int, string>();
foreach (int value in Enum.GetValues(enumerationType))
{
var name = Enum.GetName(enumerationType, value);
dictionary.Add(value, name);
}
return dictionary;
}
}
Przykład: użycie klasy narzędziowej do powiązania danych wyliczenia z kontrolką
ddlResponse.DataSource = Enumeration.GetAll<Response>();
ddlResponse.DataTextField = "Value";
ddlResponse.DataValueField = "Key";
ddlResponse.DataBind();
Moja wersja jest tylko skompresowaną formą powyższego:
foreach (Response r in Enum.GetValues(typeof(Response)))
{
ListItem item = new ListItem(Enum.GetName(typeof(Response), r), r.ToString());
DropDownList1.Items.Add(item);
}
public enum Color
{
RED,
GREEN,
BLUE
}
Każdy typ wyliczenia pochodzi od System.Enum. Istnieją dwie statyczne metody, które pomagają powiązać dane z kontrolką listy rozwijanej (i pobrać wartość). Są to Enum.GetNames i Enum.Parse. Używając GetNames, możesz powiązać się z kontrolką listy rozwijanej w następujący sposób:
protected System.Web.UI.WebControls.DropDownList ddColor;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ddColor.DataSource = Enum.GetNames(typeof(Color));
ddColor.DataBind();
}
}
Teraz, jeśli chcesz, aby wartość Enum z powrotem przy wyborze ...
private void ddColor_SelectedIndexChanged(object sender, System.EventArgs e)
{
Color selectedColor = (Color)Enum.Parse(typeof(Color),ddColor.SelectedValue
}
Po przeczytaniu wszystkich postów wymyśliłem kompleksowe rozwiązanie wspierające wyświetlanie opisu wyliczenia na liście rozwijanej, a także wybieranie odpowiedniej wartości z modelu w menu rozwijanym podczas wyświetlania w trybie edycji:
enum:
using System.ComponentModel;
public enum CompanyType
{
[Description("")]
Null = 1,
[Description("Supplier")]
Supplier = 2,
[Description("Customer")]
Customer = 3
}
klasa rozszerzenia enum:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
public static class EnumExtension
{
public static string ToDescription(this System.Enum value)
{
var attributes = (DescriptionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
public static IEnumerable<SelectListItem> ToSelectList<T>(this System.Enum enumValue)
{
return
System.Enum.GetValues(enumValue.GetType()).Cast<T>()
.Select(
x =>
new SelectListItem
{
Text = ((System.Enum)(object) x).ToDescription(),
Value = x.ToString(),
Selected = (enumValue.Equals(x))
});
}
}
Klasa modelu:
public class Company
{
public string CompanyName { get; set; }
public CompanyType Type { get; set; }
}
i Wyświetl:
@Html.DropDownListFor(m => m.Type,
@Model.Type.ToSelectList<CompanyType>())
a jeśli używasz tego menu bez powiązania z modelem, możesz użyć tego zamiast tego:
@Html.DropDownList("type",
Enum.GetValues(typeof(CompanyType)).Cast<CompanyType>()
.Select(x => new SelectListItem {Text = x.ToDescription(), Value = x.ToString()}))
W ten sposób możesz oczekiwać, że na liście rozwijanej zostanie wyświetlony opis zamiast wartości wyliczeniowych. Również jeśli chodzi o Edycję, Twój model zostanie zaktualizowany poprzez rozwijanie wybranej wartości po opublikowaniu strony.
Jak już powiedzieli inni - nie należy wiązać danych z wyliczeniem, chyba że w zależności od sytuacji konieczne jest powiązanie z różnymi wyliczeniami. Można to zrobić na kilka sposobów, kilka przykładów poniżej.
ObjectDataSource
Deklaratywny sposób na zrobienie tego z ObjectDataSource. Najpierw utwórz klasę BusinessObject, która zwróci List w celu powiązania DropDownList z:
public class DropDownData
{
enum Responses { Yes = 1, No = 2, Maybe = 3 }
public String Text { get; set; }
public int Value { get; set; }
public List<DropDownData> GetList()
{
var items = new List<DropDownData>();
foreach (int value in Enum.GetValues(typeof(Responses)))
{
items.Add(new DropDownData
{
Text = Enum.GetName(typeof (Responses), value),
Value = value
});
}
return items;
}
}
Następnie dodaj trochę znaczników HTML do strony ASPX, aby wskazać tę klasę BO:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Text" DataValueField="Value">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetList" TypeName="DropDownData"></asp:ObjectDataSource>
Ta opcja nie wymaga żadnego kodu.
Code Behind DataBind
Aby zminimalizować kod HTML na stronie ASPX i wykonać powiązanie w kodzie za:
enum Responses { Yes = 1, No = 2, Maybe = 3 }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (int value in Enum.GetValues(typeof(Responses)))
{
DropDownList1.Items.Add(new ListItem(Enum.GetName(typeof(Responses), value), value.ToString()));
}
}
}
W każdym razie, sztuczka polega na tym, aby metody typu Enum GetValues, GetNames itp. Działały za Ciebie.
Nie jestem pewien, jak to zrobić w ASP.NET, ale sprawdź ten post ... to może pomóc?
Enum.GetValues(typeof(Response));
Możesz użyć linq:
var responseTypes= Enum.GetNames(typeof(Response)).Select(x => new { text = x, value = (int)Enum.Parse(typeof(Response), x) });
DropDownList.DataSource = responseTypes;
DropDownList.DataTextField = "text";
DropDownList.DataValueField = "value";
DropDownList.DataBind();
Array itemValues = Enum.GetValues(typeof(TaskStatus));
Array itemNames = Enum.GetNames(typeof(TaskStatus));
for (int i = 0; i <= itemNames.Length; i++)
{
ListItem item = new ListItem(itemNames.GetValue(i).ToString(),
itemValues.GetValue(i).ToString());
ddlStatus.Items.Add(item);
}
public enum Color
{
RED,
GREEN,
BLUE
}
ddColor.DataSource = Enum.GetNames(typeof(Color));
ddColor.DataBind();
Kod ogólny wykorzystujący odpowiedź szóstą.
public static void BindControlToEnum(DataBoundControl ControlToBind, Type type)
{
//ListControl
if (type == null)
throw new ArgumentNullException("type");
else if (ControlToBind==null )
throw new ArgumentNullException("ControlToBind");
if (!type.IsEnum)
throw new ArgumentException("Only enumeration type is expected.");
Dictionary<int, string> pairs = new Dictionary<int, string>();
foreach (int i in Enum.GetValues(type))
{
pairs.Add(i, Enum.GetName(type, i));
}
ControlToBind.DataSource = pairs;
ListControl lstControl = ControlToBind as ListControl;
if (lstControl != null)
{
lstControl.DataTextField = "Value";
lstControl.DataValueField = "Key";
}
ControlToBind.DataBind();
}
Po znalezieniu tej odpowiedzi wymyśliłem, co uważam za lepszy (przynajmniej bardziej elegancki) sposób na zrobienie tego, pomyślałem, że wrócę i podzielę się tym tutaj.
Ładowanie strony:
DropDownList1.DataSource = Enum.GetValues(typeof(Response));
DropDownList1.DataBind();
LoadValues:
Response rIn = Response.Maybe;
DropDownList1.Text = rIn.ToString();
SaveValues:
Response rOut = (Response) Enum.Parse(typeof(Response), DropDownList1.Text);
To chyba stare pytanie… ale tak zrobiłem swoje.
Model:
public class YourEntity
{
public int ID { get; set; }
public string Name{ get; set; }
public string Description { get; set; }
public OptionType Types { get; set; }
}
public enum OptionType
{
Unknown,
Option1,
Option2,
Option3
}
Następnie w widoku: oto jak używać wypełnij listę rozwijaną.
@Html.EnumDropDownListFor(model => model.Types, htmlAttributes: new { @class = "form-control" })
Powinno to wypełnić wszystko na liście wyliczeń. Mam nadzieję że to pomoże..
[Display(Name = "Option number one")] Option1,
Nie do końca tego szukasz, ale może pomóc:
http://blog.jeffhandley.com/archive/2008/01/27/enum-list-dropdown-control.aspx
Dlaczego nie użyć w ten sposób, aby móc przekazać każdą listControle:
public static void BindToEnum(Type enumType, ListControl lc)
{
// get the names from the enumeration
string[] names = Enum.GetNames(enumType);
// get the values from the enumeration
Array values = Enum.GetValues(enumType);
// turn it into a hash table
Hashtable ht = new Hashtable();
for (int i = 0; i < names.Length; i++)
// note the cast to integer here is important
// otherwise we'll just get the enum string back again
ht.Add(names[i], (int)values.GetValue(i));
// return the dictionary to be bound to
lc.DataSource = ht;
lc.DataTextField = "Key";
lc.DataValueField = "Value";
lc.DataBind();
}
Używanie jest tak proste, jak:
BindToEnum(typeof(NewsType), DropDownList1);
BindToEnum(typeof(NewsType), CheckBoxList1);
BindToEnum(typeof(NewsType), RadoBuuttonList1);
ASP.NET został od tego czasu zaktualizowany o więcej funkcji i możesz teraz używać wbudowanego wyliczenia do rozwijania.
Jeśli chcesz powiązać sam Enum, użyj tego:
@Html.DropDownList("response", EnumHelper.GetSelectList(typeof(Response)))
Jeśli wiążesz wystąpienie Response, użyj tego:
// Assuming Model.Response is an instance of Response
@Html.EnumDropDownListFor(m => m.Response)
To jest moje rozwiązanie do zamawiania wyliczenia i wiązania danych (tekst i wartość) do listy rozwijanej przy użyciu LINQ
var mylist = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().ToList<MyEnum>().OrderBy(l => l.ToString());
foreach (MyEnum item in mylist)
ddlDivisao.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
Sprawdź mój post dotyczący tworzenia niestandardowego pomocnika „ASP.NET MVC - tworzenie pomocnika DropDownList dla wyliczeń”: http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc -creating-a-dropdownlist-helper-for-enums.aspx
Jeśli chciałbyś mieć bardziej przyjazny dla użytkownika opis w swoim polu kombi (lub innej kontrolce), możesz użyć atrybutu Opis z następującą funkcją:
public static object GetEnumDescriptions(Type enumType)
{
var list = new List<KeyValuePair<Enum, string>>();
foreach (Enum value in Enum.GetValues(enumType))
{
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).First();
if (attribute != null)
{
description = (attribute as DescriptionAttribute).Description;
}
list.Add(new KeyValuePair<Enum, string>(value, description));
}
return list;
}
Oto przykład wyliczenia z zastosowanymi atrybutami opisu:
enum SampleEnum
{
NormalNoSpaces,
[Description("Description With Spaces")]
DescriptionWithSpaces,
[Description("50%")]
Percent_50,
}
Następnie Bind, aby kontrolować w ten sposób ...
m_Combo_Sample.DataSource = GetEnumDescriptions(typeof(SampleEnum));
m_Combo_Sample.DisplayMember = "Value";
m_Combo_Sample.ValueMember = "Key";
W ten sposób możesz umieścić dowolny tekst na liście rozwijanej bez konieczności wyglądania jak nazwa zmiennej
Możesz także użyć metod rozszerzających. Dla tych, którzy nie są zaznajomieni z rozszerzeniami, radzę sprawdzić dokumentację VB i C # .
Rozszerzenie VB:
Namespace CustomExtensions
Public Module ListItemCollectionExtension
<Runtime.CompilerServices.Extension()> _
Public Sub AddEnum(Of TEnum As Structure)(items As System.Web.UI.WebControls.ListItemCollection)
Dim enumerationType As System.Type = GetType(TEnum)
Dim enumUnderType As System.Type = System.Enum.GetUnderlyingType(enumType)
If Not enumerationType.IsEnum Then Throw New ArgumentException("Enumeration type is expected.")
Dim enumTypeNames() As String = System.Enum.GetNames(enumerationType)
Dim enumTypeValues() As TEnum = System.Enum.GetValues(enumerationType)
For i = 0 To enumTypeNames.Length - 1
items.Add(New System.Web.UI.WebControls.ListItem(saveResponseTypeNames(i), TryCast(enumTypeValues(i), System.Enum).ToString("d")))
Next
End Sub
End Module
End Namespace
Aby skorzystać z rozszerzenia:
Imports <projectName>.CustomExtensions.ListItemCollectionExtension
...
yourDropDownList.Items.AddEnum(Of EnumType)()
Rozszerzenie C #:
namespace CustomExtensions
{
public static class ListItemCollectionExtension
{
public static void AddEnum<TEnum>(this System.Web.UI.WebControls.ListItemCollection items) where TEnum : struct
{
System.Type enumType = typeof(TEnum);
System.Type enumUnderType = System.Enum.GetUnderlyingType(enumType);
if (!enumType.IsEnum) throw new Exception("Enumeration type is expected.");
string[] enumTypeNames = System.Enum.GetNames(enumType);
TEnum[] enumTypeValues = (TEnum[])System.Enum.GetValues(enumType);
for (int i = 0; i < enumTypeValues.Length; i++)
{
items.add(new System.Web.UI.WebControls.ListItem(enumTypeNames[i], (enumTypeValues[i] as System.Enum).ToString("d")));
}
}
}
}
Aby skorzystać z rozszerzenia:
using CustomExtensions.ListItemCollectionExtension;
...
yourDropDownList.Items.AddEnum<EnumType>()
Jeśli chcesz ustawić wybrany element w tym samym czasie, zamień
items.Add(New System.Web.UI.WebControls.ListItem(saveResponseTypeNames(i), saveResponseTypeValues(i).ToString("d")))
z
Dim newListItem As System.Web.UI.WebControls.ListItem
newListItem = New System.Web.UI.WebControls.ListItem(enumTypeNames(i), Convert.ChangeType(enumTypeValues(i), enumUnderType).ToString())
newListItem.Selected = If(EqualityComparer(Of TEnum).Default.Equals(selected, saveResponseTypeValues(i)), True, False)
items.Add(newListItem)
Konwersja do System.Enum pozwala uniknąć problemów z rozmiarem int i wynikami. Na przykład 0xFFFF0000 będzie równe 4294901760 jako uint, ale będzie to -65536 jako int.
TryCast i as System.Enum są nieco szybsze niż Convert.ChangeType (enumTypeValues [i], enumUnderType) .ToString () (12:13 w moich testach szybkości).
Samouczek asp.net i WinForms z ComboBox i rozwijaną listą: Jak używać Enum z Combobox w C # WinForms i Asp.Net
nadzieja pomaga
Możesz to zrobić znacznie krócej
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);
}
}
}