Asp.net 将数据库数据绑定到dropdownlist
我已经尝试了很长一段时间来修复我在项目中遇到的DropDownList错误。目前,我正在使用此数据获取数据,它运行良好:Asp.net 将数据库数据绑定到dropdownlist,asp.net,sql,linq,drop-down-menu,Asp.net,Sql,Linq,Drop Down Menu,我已经尝试了很长一段时间来修复我在项目中遇到的DropDownList错误。目前,我正在使用此数据获取数据,它运行良好: using (InfoEntities ie = new InfoEntities()) { var sqlddl = (from query in ie.Table_Customers from q in ie.Accounting_PriceType
using (InfoEntities ie = new InfoEntities())
{
var sqlddl = (from query in ie.Table_Customers
from q in ie.Accounting_PriceType
where query.TypeID == 1 && q.ID == 1
orderby query.Customers
select query).Distinct().ToList();
DropDownListCust.DataTextField = "Customers";
DropDownListCust.DataValueField = "ID";
DropDownListCust.DataSource = sqlddl;
DropDownListCust.DataBind();
}
现在,当用户保存数据并再次打开网站时,我需要先前检索的dropdownlist上选择的保存值。这也很好,但问题是我得到了重复。不管怎样,我是这样做的,我很确定我做错了:
在页面加载中,我加载我的dropdownlist以获取所有项目,再加上以下内容以获取保存的值:
DropDownListCust.SelectedItem.Text = sql.Customers;
这使得我的DDL非常有缺陷,有些项目不相似,有时还会重复值。有人能帮忙吗?我正在使用LINQ,但我可以使用其他一些方法,只要它是固定的
干杯我这样解决了我的问题:
DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(sql.Customer).Selected = true;