将多个值绑定到ASP.NET中DropDownList的DataTextField [英] Bind Multiple Values to DataTextField of DropDownList in ASP.NET
本文介绍了将多个值绑定到ASP.NET中DropDownList的DataTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var query = (from Customer in context.tbl_Customer
select new
{
Value = Customer.CustomerID,
Text = Customer.CName
});
return query;
以上查询返回值和文字
Above query returns value and text
public static void FillDDLCustomer(DropDownList ddl, Boolean is_Select, Boolean is_All)
{
try
{
ddl.DataSource = _ComboRepo.FillDDLCustomer();
ddl.DataValueField = "Value";
ddl.DataTextField = "Text"; //Here I wanna bind Text as well as Value
ddl.DataBind();
if (is_Select)
{
ddl.Items.Insert(0, new ListItem("------Select------", "0"));
}
else
{
ddl.Items.Insert(0, new ListItem("------Select All------", "0"));
}
}
catch (Exception ex) { }
}
如何将返回的文本和值绑定到DataTextField?
How to bind returned text and Value to DataTextField?
推荐答案
试试下面的代码
Try the below code
var query = (from Customer in context.tbl_Customer
select new
{
Value = Customer.CustomerID,
Text=string.Format("{0} {1}" ,Customer.CustomerID,Customer.CName)
});
return query;
希望有所帮助
Hope it helps
按照以下方式执行
Do it following way
if (is_Select)
{
ddl.Items.Insert(0, new ListItem { Text = "------Select------", Value = "0" });
}
else
{
ddl.Items.Insert(0, new ListItem { Text = "------Select All------", Value = "0" });
}
这篇关于将多个值绑定到ASP.NET中DropDownList的DataTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文