/// <summary> /// 将 Enum 的值放入 dropdown /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ddl"></param> /// <param name="valueAsInteger"></param> public static void FromEnum <T>(this DropDownList ddl, Nullable <T> defaultValue = null, Func <T, ListItem, bool> itemSet = null, bool valueAsInteger = false) where T : struct, IComparable, IConvertible, IFormattable { ddl.Items.Clear(); DefinitionHelper.GenerateEnums <T>().ForEach(i => { var item = new ListItem() { Text = i.Caption, Value = i.Name }; var v = i.Value.ToIntOrNull().Value; if (valueAsInteger) { item.Value = v.ToString(); } item.Selected = (defaultValue.HasValue && defaultValue.Value.Equals(v)); if (itemSet != null) { if (itemSet((T)(v as object), item)) { ddl.Items.Add(item); } } else { ddl.Items.Add(item); } }); }
/// <summary> /// 将 Enum 的值放入 dropdown /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ddl"></param> /// <param name="valueAsInteger"></param> public static void FromEnum <T>(this ASPxComboBox cbx, Nullable <T> defaultValue = null, Func <T, ListEditItem, bool> itemSet = null, bool valueAsInteger = false, bool canBeNull = false) where T : struct, IComparable, IConvertible, IFormattable { cbx.Items.Clear(); DefinitionHelper.GenerateEnums <T>().ForEach(i => { var item = new ListEditItem() { Text = i.Caption, Value = i.Name }; var v = i.Value.ToIntOrNull().Value; if (valueAsInteger) { item.Value = v; } if (defaultValue.HasValue) { if (valueAsInteger) { cbx.Value = (int)(defaultValue.Value as object); } else { cbx.Value = defaultValue.Value; } } if (itemSet != null) { if (itemSet((T)(v as object), item)) { cbx.Items.Add(item); } } else { cbx.Items.Add(item); } }); if (canBeNull) { if (cbx.Buttons.Count == 0) { var b = cbx.Buttons.Add(); b.Index = 0; b.Text = "X"; b.Width = 15; } if (string.IsNullOrEmpty( cbx.ClientSideEvents.ButtonClick)) { cbx.ClientSideEvents.ButtonClick = "function(s,e){{s.SelectIndex(-1);}}"; } } }
public DefinitionItemHelper(Type type) { _Items.AddRange(DefinitionHelper.GenerateItems(type)); _MasterItem = DefinitionHelper.GenerateItem(type); }