[英]How to populate DropDownList in asp with enum possible values from a MySQL Database
In my MySQL Database I have a column of type enum with predefined values. 在我的MySQL数据库中,我有一列类型为enum的预定义值。 The column's name is Category and the values are 'movie','gaming', 'food', ... and so on.
该列的名称为“类别”,其值为“电影”,“游戏”,“食物”等。 And now in my aspx page, there's a asp DropDownList that I would like to populate with the enum possible values.
现在在我的aspx页面中,有一个我要用枚举可能的值填充的asp DropDownList。
So In my code behind I would like to get these possible values and populate my DropDownList with them 所以在我后面的代码中,我想获取这些可能的值并用它们填充我的DropDownList
I would like if you you guys would lead me to the right path, thank you very much! 我希望你们能带领我走上正确的道路,非常感谢!
You can bind the data directly from the database and add the ListItems to the DropDownList. 您可以直接从数据库绑定数据,并将ListItems添加到DropDownList。
while (reader.Read())
{
DropDownList1.Items.Insert(i, new ListItem(reader["Text"].ToString(), reader["Valye"].ToString(), true));
i++;
}
Or you can bind an already exisiting source that has been populated somewhere else 或者,您可以绑定已经存在于其他地方的现有资源
DataTable source = new DataTable();
List<myClass> source = new List<myClass>();
DropDownList1.DataSource = source;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
Or use an SqlDataSource
或使用
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.