// 僅名稱
DropDownList1.DataSource = Enum.GetNames(typeof(OrderStatus));
DropDownList1.DataBind();
// 需要值:轉換為鍵值對
var items = Enum.GetValues(typeof(OrderStatus))
.Cast()
.Select(e => new ListItem(e.ToString(), ((int)e).ToString()));
DropDownList1.DataSource = items;
DropDownList1.DataBind();
var logistics = new LogisticsBLL().GetAll(); // List
DropDownList3.DataSource = logistics;
DropDownList3.DataTextField = "Name"; // 顯示字段
DropDownList3.DataValueField = "Id"; // 值字段(通常為主鍵)
DropDownList3.DataBind();
關鍵:DataTextField/DataValueField 必須匹配對象屬性名(區分大小寫)。
DropDownList3.DataBind();
DropDownList3.Items[0].Text = ""; // 丟失原第一條數據!
DropDownList3.DataSource = logistics;
DropDownList3.DataTextField = "Name";
DropDownList3.DataValueField = "Id";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, new ListItem("請選擇", "")); // 索引 0 插入
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, new ListItem("請選擇", ""));
// 根據業務邏輯設置
if (defaultLogisticsId > 0)
DropDownList3.SelectedValue = defaultLogisticsId.ToString();