你试着从城市数据库中填充组合框,但我想选择菜单顶部的大城市 我使用过这种方法我有什么选择吗?
对于地铁:
List<Lst_City> lstCity= new List<Lst_City>();
lstCity = new BFCommon().getCities();
lstCity[0].CityID = 474;
lstCity[0].CityNM = "Mumbai";
lstCity[1].CityID = 199;
lstCity[1].CityNM = "Delhi";
lstCity[2].CityID = 165;
lstCity[2].CityNM = "Chennai";
lstCity[3].CityID = 384;
lstCity[3].CityNM = "Kolkata";
lstCity[4].CityID = 582;
lstCity[4].CityNM = "Pune";
lstCity[5].CityID = 71;
lstCity[5].CityNM = "Bangalore";
lstCity[6].CityID = 306;
lstCity[6].CityNM = "Hyderabad";
lstCity[7].CityID = 11;
lstCity[7].CityNM = "Ahmedabad";
下拉绑定:
ddCities.DataSource = lstCity;
ddCities.DataTextField = "CityNM";
ddCities.DataValueField = "CityID";
ddCities.DataBind();
ddCities.Items.Insert(0, new ListItem("Select One", string.Empty));
查询:
public List<Lst_City> getCities()
{
List<Lst_City> temp = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true
select e).ToList();
return temp;
}
和设计器中的组合框
<div class="row">
<label>
City :</label>
<ajaxToolkit:ComboBox ID="ddCities" runat="server" AutoPostBack="False"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append" Width="380px"></ajaxToolkit:ComboBox>
我正在实现这个目的,但由于硬编码和条目重复,它似乎并不理想
答案 0 :(得分:0)
从你的代码看来,所有城市都在一张桌子里。
我的建议是:
添加新列可能是IsMetropolital
List<Lst_City> temp = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true && e.IsMetropolital==false
select e).ToList();
List<Lst_MetroCity> tempMetro = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true && e.IsMetropolital=true
select e).ToList();
List<Lst_City> lstCity= new List<Lst_City>();
foreach(var t in tempMetro)
{
// Add cities to the lstCity
}
foreach(var t in temp)
{
// Add cities to the lstCity
}
现在终于使用lstCity填充下拉列表的数据源。