我将一个dropdownlist绑定到数据源,并在codebehind上定义了text和value列。
DropDownList1.DataTextField = "Product_Name"
DropDownList1.DataValueField = "Product_ID"它工作得很好。但是我想再给DataTextField赋值一列,Order_Date。我尝试了我的机会:
DropDownList1.DataTextField = "Product_Name"+"Order_Date"
DropDownList1.DataValueField = "Product_ID"但它并没有起作用。是否可以在DataTextField上显示多个值
发布于 2013-11-12 20:26:24
是的,这是可能的,尝试使用以下代码:
var datasource = from x in products
select new {
x.Id,
x.Code,
x.Description,
DisplayField = String.Format("{0} ({1})", x.Code, x.Description)
};
DropDownList1.DataSource = datasource;
DropDownList1.DataValueField = "Id";
DropDownList1.DataTextField = "DisplayField";
DropDownList1.DataBind();发布于 2013-11-13 20:51:23
事实证明,最好的方法是按照Freak_Droid的建议直接从数据库中获取价值,我也遵循了他的方法。
发布于 2016-05-16 23:13:36
我使用对象datasource填充dropdownlist,并在模型中创建了一个新属性
public string Datatextfield
{
get
{
return Name + " - Date: " + Date.ToShortDateString();
}
set { Datatextfield = value; }
}在aspx中,我像这样使用该属性:
<asp:DropDownList ID="ddlList" DataSourceID="obj" runat="server" DataTextField="Datatextfield" DataValueField="ID">https://stackoverflow.com/questions/19928911
复制相似问题