格式 DropDownList.TextValue [英] Format DropDownList.TextValue

查看:25
本文介绍了格式 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆