我在ASP.NET页面中有一个名为(ddl)的下拉列表,我希望DDL在数据库中包含一些表的记录。

所以我做到了:

DDL.DataSource = myDataReader

DDL.DataBind()

但这给了我(5个记录)“表的记录数”,但是这样:

System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
有帮助吗?

解决方案

您应该设置datatextfield和datavaluefield,否则数据绑定将在每一行上执行.tostring(),并将其作为项目:

DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();

其他提示

您必须在databind之前设置DDL的文本和关键字段

DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":

编码 : ddl.datasource=reader 只是将读者中存在的内容(表的列数组)作为数据的主要来源。
现在 ddl 其中只显示一个列,所以您需要编写一件代码 ddl 它必须显示哪个列。
所以你会写: ddl.textfield=“您要显示的列名”;和 ddl.valuefield="您希望将其作为传递到数据库的参考名称“;

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top