DropDownList OnSelectedIndexChanged不是Firing

时间:2013-09-09 18:22:37

标签: c# asp.net

我的主要目的是根据ddlMedicalName选择填充ddlCity DropDownList。但我的主要问题是,虽然我几乎尝试了解决该问题的所有解决方案,但我无法解决这个问题,因为我无法解雇ddlMedicalName_OnSelectedIndexChanged。

我知道,关于这个问题有很多问题和答案,我已经尝试了almos中的每一个,但它仍然没有用。我已经读过,EnableStateView = True不仅应该应用于DropDownList,还应该应用于整个页面,所以我将以下代码添加到Web.Config:

 <system.web>
<pages styleSheetTheme="MainTheme" enableViewState="true"/>

这是我背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;



namespace MedicalBootStrap
{

public partial class WebForm1 : System.Web.UI.Page
{
    Entity.medicaldbEntities context = new Entity.medicaldbEntities();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            ddlBind();
        }

    }

    protected void ddlBind()
    {

        var orders = from order in context.Medicals

                     select new { order.MedicalID, order.medicalName, order.CityFK };

        var lstsrc = orders.ToList();

        ddlMedicalName.DataSource = lstsrc;
        ddlMedicalName.DataTextField = "medicalname";
        ddlMedicalName.DataValueField = "cityfk";

        ddlMedicalName.DataBind();

    }

    protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
    {

          int slc = Convert.ToInt32(ddlMedicalName.SelectedItem.Value);

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

        var lstsrc = orders.ToList();

        ddlMedicalName.DataSource = lstsrc;
        ddlMedicalName.DataTextField = "medicalname";
        ddlMedicalName.DataValueField = "cityfk";

        ddlMedicalName.DataBind();
    }

    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {

    }   
 }
}

这是aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MedizinischeLeistungen.aspx.cs" Inherits="MedicalBootStrap.WebForm1" EnableViewState="true" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" EnableViewState="true">
<table id="SearchTable">
<tr>
<td>Medizinische Leistung</td>
<td>Region</td>
<td>Stadt</td>
</tr>
<tr id="SearchDropDown">

<td>
    <asp:DropDownList ID="ddlMedicalName" runat="server" EnableViewState="true" 
        AutoPostBack="True"  AppendDataBoundItems="true"
    OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged">
    <asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
    </asp:DropDownList>
</td>

<td>
    <asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true" EnableViewState="true"
onselectedindexchanged="ddlRegion_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
    </asp:DropDownList>
</td>
<td>
    <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" EnableViewState="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
    </asp:DropDownList>
</td>
<td>
    <asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />

</td>
</tr>
</table>




</asp:Content>

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:2)

我测试了你的代码。这是我到目前为止所发现的 -

ddlMedicalName_OnSelectedIndexChanged 事件被正确触发。

ddlMedicalName.SelectedValue 是一个字符串值; 字符串不是 ddlCity.DataSource的有效数据源

protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
    // Problem is the following 3 line of codes.
    ddlCity.DataSource = ddlMedicalName.SelectedValue; 
    ddlCity.DataTextField = ddlMedicalName.SelectedValue;
    ddlCity.DataValueField = ddlMedicalName.SelectedValue;

    ddlCity.DataBind();
}

更新

以下是我的测试方法 -

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        ddlBind();
    }
}

public class Medical
{
    public int MedicalID { get; set; }
    public string MedicalName { get; set; }
    public int CityFK { get; set; }
}

public class City
{
    public int CityID { get; set; }
    public string CityName { get; set; }
}

protected void ddlBind()
{
    var medicals = new List<Medical>()
    {
        new Medical {MedicalID = 1, MedicalName = "One", CityFK = 11},
        new Medical {MedicalID = 2, MedicalName = "Two", CityFK = 22},
        new Medical {MedicalID = 3, MedicalName = "Three", CityFK = 33},
        new Medical {MedicalID = 4, MedicalName = "Four", CityFK = 44},
    };

    ddlMedicalName.DataSource = medicals;
    ddlMedicalName.DataTextField = "medicalname";
    ddlMedicalName.DataValueField = "cityfk";

    ddlMedicalName.DataBind();

}

protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
    var cities = new List<City>()
    {
        new City {CityID = 11, CityName = "City One"},
        new City {CityID = 22, CityName = "City Two"},
        new City {CityID = 33, CityName = "City Three"},
        new City {CityID = 44, CityName = "City Four"},
    };

    int slc = Convert.ToInt32(ddlMedicalName.SelectedItem.Value);

    var orders = from order in cities
                    where order.CityID == slc
                    select new { order.CityID, order.CityName };

    var lstsrc = orders.ToList();

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

    ddlCity.DataBind();
}

protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void SearchButton_Click(object sender, EventArgs e)
{

}

答案 1 :(得分:0)

请改变我的建议并尝试。您必须删除以下行

ddlMedicalName.DataValueField = "cityfk";

将cityfk更改为medicalname并尝试。

ddlMedicalName.DataValueField = "medicalname";

答案 2 :(得分:-1)

变化:

protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{

为:

protected void ddlMedicalName_SelectedIndexChanged(object sender, EventArgs e)
{

或相反:

<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" EnableViewState="true" onselectedindexchanged="ddlCity_SelectedIndexChanged">

为:

<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"  EnableViewState="true" onselectedindexchanged="ddlCity_OnSelectedIndexChanged">

您当前指向名为“..._ SelectedIndexChanged”的句柄,并提供“..._ OnSelectedIndexChanged”。