0

我不知道为什么 ddlMedicalName 的值更改时没有填充 ddlCity 。我只是想构建函数“ddlMedicalName_SelectedIndexChanged”,以便如果有人从下拉列表 ddlMedicalName 中选择任何 MedicalName,则应根据 ddlMedicalName 的选定值填充另一个下拉列表“ddlCity”数据源。

后面的代码:

  protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        //string entityString = ConfigurationManager.ConnectionStrings["medicaldb2Entities"].ConnectionString;
        //EntityDataSource eds = new EntityDataSource();
        using (Entity.medicaldbEntities context = new Entity.medicaldbEntities())
        {
            string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

            var orders = from order in context.Cities
                         where order.CityID == slct
                         select new { order.CityID, order.CityName };

            var lstSrc = orders.ToList();

            ddlCity.DataSource = lstSrc;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";

            ddlCity.DataBind();
        }

    }

ASPX:

<tr id="SearchDropDown">

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldbEntities"  
    DefaultContainerName="medicaldbEntities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName],it.[CityFK]">
 </asp:EntityDataSource>

<td>
<asp:DropDownList ID="ddlMedicalName" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName"
    DataValueField="CityFK">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlRegion_SelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName" 
    DataValueField="MedicalName">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
     DataTextField="CityID" 
    DataValueField="CityID">
</asp:DropDownList>
</td>
<td>
<asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />
</td>
</tr>

这就是我得到的错误:

在此处输入图像描述

如果我将其更改为:

List<string> lstSrc = orders.ToList();

它不会抛出任何错误,但它也不起作用(ddlCity 未填充)。

4

3 回答 3

3

您准备query但从未实际使用它。

var orders = from order in context.Cities
             where order.CityID == slct
             select order.CityName;

List<Entity.City> lstSrc = orders.ToList();

编辑

另一件事是,您不仅CityName要从查询中获取更多信息:

var orders = from order in context.Cities
             where order.CityID == slct
             select new { order.CityID, order.CityName };

它将返回具有两个属性的匿名类型:CityIDCityName. var但是因为它是匿名类型,所以在声明List<T>变量时必须使用:

var lstSrc = orders.ToList();

或将其直接分配给DataSource财产:

ddlCity.DataSource = orders.ToList();
于 2013-09-07T16:28:09.303 回答
1
string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

            var orders = from order in context.Cities
                         where order.CityID == slct
                         select order.CityName;

            ddlCity.DataSource = orders.ToList();
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();

检查 Autopostback="true"

<asp:Dropdownlist id="ddlMedicalName" runat="Server" Autopostback="true" ></Dropdownlist>
于 2013-09-07T16:45:17.070 回答
0

我的问题非常具体;在 master.site 的主要表单标签有一个警告。表单的结束标签已经存在,但由于一些不允许的控件或标签,它没有被检测到并且没有呈现,这导致了那个奇怪的下拉列表问题的原因。然后我编辑了我的 html 和主表单标签,摆脱了“Form1 没有结束标签”的警告,一切都恢复正常了。

于 2014-03-21T09:39:38.170 回答