格式DropDownList.TextValue格式、DropDownList、TextValue

2023-09-03 11:27:07 作者:你tm的、滚

我的存储过程是这样的

 选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;
 

看来,这不是自动实现的,例如:看this MS连接票。

这样做的编程方式:

  forecach(在table.Rows VAR行)
{
    row.Field<字符串>(文本)=的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(..); }; 
}