格式 DropDownList.TextValue [英] Format DropDownList.TextValue
问题描述
我的存储过程是这样的
SELECT Id, StudentName
FROM xyz
我在 asp.net 中有一个下拉列表,我将其加载为:
I have a drop down list in asp.net, which I am loading as :
ddlA.DataSource = // Some source
ddlA.DataTextField = "Id" + " -" + "StudentName";
ddlA.DataValueField = "Id";
ddlA.DataBind();
ddlA.Items.Insert(0, new ListItem(" Select one", "0"));
但是在 Databind()
语句中,我收到此错误:
But at the Databind()
statement, I am getting this error:
System.Web.HttpException: DataBinding: 'System.Data.DataRowView' 不包含名为 'Id-StudentName' 的属性.
System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Id-StudentName'.
在下拉列表的文本部分,我想显示Id - StudentName
的串联值.
In text part of the dropdown list, I want to display the concatenated value of Id - StudentName
.
我该怎么做?
推荐答案
DropDownList1.DataTextFormatString = "{0} - {1}";
DropDownList1.DataTextField = "Id,StudentName";
似乎无法自动实现,例如请参阅 此 MS Connect门票.
It seems that it's not achievable automatically, e.g. see this MS Connect ticket.
因此以编程方式执行此操作:
Thus do that programmatically:
foreach (var row in table.Rows)
{
row.Field<string>("text") = String.Format(..);
}
或
foreach (var item in data)
{
new ListItem { Text = String.Format(..); };
}
这篇关于格式 DropDownList.TextValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!