简体   繁体   中英

EntityDataSource both on ASP and code-behind side?

I populate my 2 dropdownlists by default on the asp side with entitydatasource control. Then whenever user selects an item from one of them, the other dropdownlist content should be populated(updated) accordingly. But when I select an item on one of the dropdownlists, I get the following error:

Both DataSource and DataSourceID are defined on 'ddlMedicalName'. Remove one definition.

Here is the asp side:

    <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> 

Here is the code-behind:

  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();

        }
    }

Most probably you don't need the relations of the database, If you need any more info please let me know.

Both DataSource and DataSourceID are defined on 'ddlMedicalName'. Remove one definition.

The error message is pretty clear, you shouldn't assign the DataSourceID to your definition AND the DataSource in your code-behind.

Remove this from your DropDownList definition:

DataSourceID="ddlMedicalName_DataSource"

Alternatively, you could remove the DataSource from the code-behind, but I envision that causing trouble on how to maintain control of when it is bound to what.

This will hold true with all of your controls which you are setting datasources for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM