简体   繁体   English

如何用来自MySQL数据库的枚举可能的值在asp中填充DropDownList

[英]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.

相关问题 如何使用枚举中的值从ASP.NET MVC Web应用程序中的模型填充下拉列表? - How can I use values from an enum to populate a dropdownlist from my model in an ASP.NET MVC web application? 单击asp.net中的[显示]按钮,如何在数据库中的下拉列表中选择选项时填充其他下拉列表? - How to populate other dropdownlist on selecting an option in a dropdownlist from database on click of a [show] button in asp.net? Asp.Net如何从另一个下拉列表填充下拉列表 - Asp.Net How to populate dropdownlist from another dropdownlist 从ASP.NET MVC中的数据库以表格形式填充下拉列表 - Populate dropdownlist from database in asp.net MVC in a form 如何使用linq从SQL数据库填充DropDownList,只选择Distinct Values - How to populate a DropDownList from a SQL database using linq and only select Distinct Values 使用数据库中的数据读取器填充下拉列表 - Populate dropdownlist with datareader from database 使用多参数从数据库填充下拉列表 - Populate dropdownlist from database with multiparameter 如何使用c#基于asp文本框中的文本使用来自sql数据库的数据填充asp下拉列表? - How do I populate an asp dropdownlist with data from an sql database based on the text from an asp textbox using c#? 如何从C#端填充asp:DropDownList - how to populate asp:DropDownList from C# side 如何从表中填充 ASP.NET Core MVC 中的下拉列表? - How to populate a dropdownlist in ASP.NET Core MVC from table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM