/// <summary> /// Eume绑定到WebControl /// </summary> /// <param name="control">要绑定的ddl</param> /// <param name="eumType">枚举的Type</param> /// <param name="defShowText">第一行要显示的文字</param> /// <param name="defShowValue">第一行要显示的值</param> /// <param name="selectedValue">默认选择值</param> public static void Bind(this ListControl control, Type eumType, object selectedValue = null, string defShowText = null, object defShowValue = null) { control.DataSource = eumType.ToListItem(); control.DataValueField = "Value"; control.DataTextField = "Text"; control.DataBind(); if (control.Items.Count > 0) { control.SelectedIndex = 0; } control.InsertItem(defShowValue, defShowText); if (selectedValue != null) { control.SelectedItems(selectedValue); } }
/// <summary> /// IEnumerable绑定到WebControl /// </summary> /// <param name="control">要绑定的ddl</param> /// <param name="lst">源数据</param> /// <param name="dataTextField">绑定的文本字段</param> /// <param name="dataValueField">绑定的值字段</param> /// <param name="defShowText">第一行显示的文字</param> /// <param name="defShowValue">第一行显示的值</param> /// <param name="selectedValue">默认选择值</param> public static void Bind(this ListControl control, IEnumerable lst, object selectedValue = null, string dataTextField = "Caption", string dataValueField = "ID", string defShowText = null, object defShowValue = null) { control.DataSource = lst; control.DataTextField = dataTextField; control.DataValueField = dataValueField; control.DataBind(); if (control is DropDownList && control.Items.Count > 0) { control.SelectedIndex = 0; } control.InsertItem(defShowValue, defShowText); if (selectedValue != null) { control.SelectedItems(selectedValue); } }