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

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

问题描述

我的存储过程是这样的

 选ID,StudentName
从XYZ
 

我有一个下拉列表,asp.net,这我加载为:

  ddlA.DataSource = //一些源
ddlA.DataTextField =ID+ - +StudentName;
ddlA.DataValueField =ID;
ddlA.DataBind();
ddlA.Items.Insert(0,新的ListItem(选择一个,0));
 

但在数据绑定()语句,我收到此错误:

  

System.Web.HttpException:数据绑定:'System.Data.DataRowView不包含名为标识,StudentName'属性。

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

我该怎么办呢?

解决方案

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

看来,这不是自动实现的,例如:看<一href="https://connect.microsoft.com/VisualStudio/feedback/details/103308/multiple-data-bound-properties-for-datatextfield-on-dropdownlist">this MS连接票。


这样做的编程方式:

  forecach(在table.Rows VAR行)
{
    row.Field&LT;字符串&GT;(文本)=的String.Format(..);
}
 

 的foreach(数据VAR项)
{
    新列表项的{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:

forecach (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天全站免登陆