您可以直接從數據庫綁定數據並將ListItems添加到DropDownList。
while (reader.Read())
{
DropDownList1.Items.Insert(i, new ListItem(reader["Text"].ToString(), reader["Valye"].ToString(), true));
i++;
}
或者你可以綁定已填充其他
DataTable source = new DataTable();
List<myClass> source = new List<myClass>();
DropDownList1.DataSource = source;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
地方已經exisiting源或使用SqlDataSource
SqlDataSource source = new SqlDataSource();
source.SelectCommand = "SELECT * FROM yourTable";
source.ConnectionString = Common.connectionString;
DropDownList1.DataSource = source;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
你有沒有嘗試過什麼? –