0

默认情况下,我在 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();

        }
    }

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

4

1 回答 1

0

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

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

从您的DropDownList定义中删除它:

DataSourceID="ddlMedicalName_DataSource"

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

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

于 2013-10-09T19:43:06.597 回答