1

我有一个 SQL 选择语句并使用结果将它绑定到一个 asp 下拉列表。

String strQuery ="SELECT RTRIM(NAME), NUMBER . . ." 

customerselect.DataTextField = "NAME+ ' | ' +NUMBER";

我需要在 DataTextField 中显示:

Example Name | 123456

我收到一个错误:

" 不包含名为 'NAME+ ' | ' +NUMBER' 的属性。"

4

3 回答 3

2

DataTextField属性指定数据源中的哪个字段用作下拉菜单的文本标签,而不是实际的文本字符串本身。因此,如果您希望下拉菜单具有该特定格式,您可以尝试以下操作:

String strQuery = "SELECT RTRIM(NAME) + ' | ' +
    CAST(NUMBER AS VARCHAR(32)) AS Label, NUMBER AS Value . . .";

// Run the query and do the appropriate data binding here

customerselect.DataTextField = "Label";

可以在此处找到有关 DataTextField 属性的其他信息:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datatextfield%28v=vs.110%29.aspx

于 2014-01-07T20:07:55.520 回答
1

尝试:

customerselect.DataTextField = string.Format("{0}+ | +{1}", Name, Number);
于 2014-01-07T19:52:11.560 回答
0

给你的函数结果一个别名。换句话说,改变这个:

String strQuery ="SELECT RTRIM(NAME), NUMBER . . ." 

对此:

String strQuery ="SELECT RTRIM(NAME) as name, NUMBER . . ." 
于 2014-01-07T19:53:59.630 回答