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

查看:127
本文介绍了格式DropDownList.TextValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的存储过程就像这样

  SELECT Id,StudentName 
FROM xyz

我在asp.net中有一个下拉列表,我正在加载:

  ddlA.DataSource = //某些来源
ddlA.DataTextField =Id+ - +StudentName;
ddlA.DataValueField =Id;
ddlA.DataBind();
ddlA.Items.Insert(0,new ListItem(Select one,0));

但是在$ code> Databind()我收到这个错误:


System.Web.HttpException:DataBinding:'System.Data.DataRowView'不包含属性名称'Id-StudentName'。


在下拉列表的文本部分,我想显示连接值 Id - StudentName



我该怎么做?

解决方案

  DropDownList1.DataTextFormatString ={0}  -  {1}; 
DropDownList1.DataTextField =Id,StudentName;

似乎这不能自动实现,例如请参阅此MS Connect票






因此以编程方式执行:



pre> foreach(table.Rows中的var row)
{
row.Field< string>(text)= String.Format(..);
}

  foreach(数据中的var项)
{
new ListItem {Text = String.Format(..); };
}


My stored procedure is like this

SELECT Id, StudentName
FROM xyz

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"));

But at the Databind() statement, I am getting this error:

System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Id-StudentName'.

In text part of the dropdown list, I want to display the concatenated value of Id - StudentName.

How can I do it?

解决方案

DropDownList1.DataTextFormatString = "{0} - {1}";
DropDownList1.DataTextField = "Id,StudentName";

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(..);
}

or

foreach (var item in data)
{
    new ListItem { Text = String.Format(..); }; 
}

这篇关于格式DropDownList.TextValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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