默认情况下,我在 asp 端使用 entitydatasource 控件填充我的 2 个下拉列表。然后,每当用户从其中一个项目中选择一个项目时,应该相应地填充(更新)另一个下拉列表内容。但是当我在其中一个下拉列表中选择一个项目时,我收到以下错误:
DataSource 和 DataSourceID 都在“ddlMedicalName”上定义。删除一个定义。
这是asp的一面:
<asp:DropDownList ID="ddlMedicalName" runat="server"
AutoPostBack="True" DataSourceID="ddlMedicalName_DataSource"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged" DataTextField="MedicalName" DataValueField="CityFK"
AppendDataBoundItems="true">
<asp:ListItem Text="-Medizinische Leistung Wählen-" Value="" ></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="ddlMedicalName_DataSource" runat="server"
ConnectionString="name=EntitiesMedical" DefaultContainerName="EntitiesMedical"
EntitySetName="Medicals">
</asp:EntityDataSource>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" DataSourceID="ddlCity_DataSource"
OnSelectedIndexChanged="ddlCity_OnSelectedIndexChanged" DataTextField="CityName" DataValueField="CityID"
AppendDataBoundItems="true">
<asp:ListItem Text="-Stadt Wählen-" Value="" ></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="ddlCity_DataSource" runat="server"
ConnectionString="name=EntitiesMedical" DefaultContainerName="EntitiesMedical"
EntitySetName="Cities">
</asp:EntityDataSource>
下面是代码隐藏:
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
using (Entity.EntitiesMedical cities = new Entity.EntitiesMedical())
{
int slcID = Convert.ToInt32(ddlMedicalName.SelectedItem.Value);
var result = from ct in cities.Cities
where ct.CityID == slcID
select new
{
CityID = ct.CityID,
CityName = ct.CityName
};
EntityDataSource eds = new EntityDataSource();
eds.ConnectionString = "name=EntitiesMedical";
eds.DefaultContainerName = "EntitiesMedical";
eds.EntitySetName = "Cities";
ddlCity.DataSource = result.ToList();
ddlCity.DataValueField = "CityID";
ddlCity.DataTextField = "CityName";
ddlCity.DataBind();
}
}
protected void ddlCity_OnSelectedIndexChanged(object sender, EventArgs e)
{
using (Entity.EntitiesMedical medicals = new Entity.EntitiesMedical())
{
int slcID = Convert.ToInt32(ddlCity.SelectedItem.Value);
var result = from med in medicals.Medicals
where med.CityFK == slcID
select new
{
MedicalID = med.MedicalID,
MedicalName = med.medicalName
};
EntityDataSource eds = new EntityDataSource();
eds.ConnectionString = "name=EntitiesMedical";
eds.DefaultContainerName = "EntitiesMedical";
eds.EntitySetName = "Medicals";
ddlMedicalName.DataSource = result.ToList();
ddlMedicalName.DataValueField = "MedicalID";
ddlMedicalName.DataTextField = "MedicalName";
ddlMedicalName.DataBind();
}
}
很可能您不需要数据库的关系,如果您需要更多信息,请告诉我。