在ASP和代码隐藏方面的EntityDataSource?

时间:2013-10-09 19:35:35

标签: c# asp.net entity-framework

我默认在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();

        }
    }

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

1 个答案:

答案 0 :(得分:0)

  

DataSource和DataSourceID都在'ddlMedicalName'上定义。删除一个定义。

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

DropDownList定义中删除此内容:

DataSourceID="ddlMedicalName_DataSource"

或者,您可以从代码隐藏中删除DataSource,但我设想在绑定到时将如何保持对的控制权。

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