我不知道为什么 ddlMedicalName 的值更改时没有填充 ddlCity 。我只是想构建函数“ddlMedicalName_SelectedIndexChanged”,以便如果有人从下拉列表 ddlMedicalName 中选择任何 MedicalName,则应根据 ddlMedicalName 的选定值填充另一个下拉列表“ddlCity”数据源。
后面的代码:
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
//string entityString = ConfigurationManager.ConnectionStrings["medicaldb2Entities"].ConnectionString;
//EntityDataSource eds = new EntityDataSource();
using (Entity.medicaldbEntities context = new Entity.medicaldbEntities())
{
string selected = ddlMedicalName.SelectedItem.Value;
int slct = Convert.ToInt32(selected);
var orders = from order in context.Cities
where order.CityID == slct
select new { order.CityID, order.CityName };
var lstSrc = orders.ToList();
ddlCity.DataSource = lstSrc;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
}
}
ASPX:
<tr id="SearchDropDown">
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=medicaldbEntities"
DefaultContainerName="medicaldbEntities" EnableFlattening="False"
EntitySetName="Medicals" Select="it.[medicalName],it.[CityFK]">
</asp:EntityDataSource>
<td>
<asp:DropDownList ID="ddlMedicalName" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged"
DataSourceID="EntityDataSource1" DataTextField="MedicalName"
DataValueField="CityFK">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlRegion_SelectedIndexChanged"
DataSourceID="EntityDataSource1" DataTextField="MedicalName"
DataValueField="MedicalName">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
DataTextField="CityID"
DataValueField="CityID">
</asp:DropDownList>
</td>
<td>
<asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />
</td>
</tr>
这就是我得到的错误:
如果我将其更改为:
List<string> lstSrc = orders.ToList();
它不会抛出任何错误,但它也不起作用(ddlCity 未填充)。