ASP 端和代码隐藏端的实体数据源

本文关键字:实体 数据源 代码 ASP 隐藏 | 更新日期: 2023-09-27 18:34:43

默认情况下,我在 asp 端使用实体数据源控件填充我的 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();
        }
    }

很可能您不需要数据库的关系,如果您需要更多信息,请告诉我。

ASP 端和代码隐藏端的实体数据源

DataSource和DataSourceID都是在"ddlMedicalName"上定义的。删除一个定义。

错误消息非常清楚,您不应该将DataSourceID分配给您的定义和代码隐藏中的DataSource

DropDownList定义中删除以下内容:

DataSourceID="ddlMedicalName_DataSource"

或者,您可以从代码隐藏中删除DataSource,但我认为这会在如何保持对何时绑定到什么的控制方面造成麻烦。

这将适用于要为其设置数据源的所有控件。