[英]Populate DataTextField with “Example Name | Example Number”
I have a SQL select statement and using the results I bind it to an asp dropdown. 我有一个SQL select语句并使用结果我将它绑定到asp下拉列表。
String strQuery ="SELECT RTRIM(NAME), NUMBER . . ."
customerselect.DataTextField = "NAME+ ' | ' +NUMBER";
I need to display in the DataTextField: 我需要在DataTextField中显示:
Example Name | 123456
I'm getting an error: 我收到一个错误:
" does not contain a property with the name 'NAME+ ' | ' +NUMBER'." “不包含名称为'NAME +'|'+ NUMBER'的属性。”
The DataTextField
property specifies which field from your data source to use as the text label for the drop-down, not the actual text string itself. DataTextField
属性指定数据源中的哪个字段用作下拉列表的文本标签,而不是实际的文本字符串本身。 So, if you wanted the drop-down to have that particular format, you might try something like the following: 因此,如果您希望下拉列表具有该特定格式,您可以尝试以下内容:
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";
Additional info on the DataTextField property can be found here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datatextfield%28v=vs.110%29.aspx 有关DataTextField属性的更多信息,请访问: http : //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datatextfield%28v=vs.110%29.aspx
尝试:
customerselect.DataTextField = string.Format("{0}+ | +{1}", Name, Number);
Give your function result an alias. 为您的函数结果添加别名。 In other words, change this: 换句话说,改变这个:
String strQuery ="SELECT RTRIM(NAME), NUMBER . . ."
to this: 对此:
String strQuery ="SELECT RTRIM(NAME) as name, NUMBER . . ."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.