为什么asp:DropDownList不响应代码隐藏

本文关键字:代码 隐藏 不响应 DropDownList asp 为什么 | 更新日期: 2023-09-27 18:03:37

我有以下ASP下拉列表:

<asp:DropDownList ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">
        <asp:ListItem Text="BY PHYSICIAN" Value="0" Selected="True" />
        <asp:ListItem Text="BY LOCATION" Value="1" />
        <asp:ListItem Text="BY SPECIALTY" Value="2" />
</asp:DropDownList>
<br /><br />
<asp:DropDownList ClientIDMode="Static" ID="ddlDrillDown" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>

我正试图使其交互,如果第一个选择选项被改变,第二个选择选项也会根据第一个选择选项的选择而改变。

我的c#代码是这样的:

public partial class test : System.Web.UI.Page
{
    String cString;
    SqlConnection Conn;
    protected void Page_Load(object sender, EventArgs e) {
    PopulatePhysician();
    //PopulateSpecialty();
    //PopulateLocation();
    }

    public void PopulatePhysician() {
        SqlCommand cmd = new SqlCommand("getPhysicians", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
        //cmd.CommandType = Data.CommandType.StoredProcedure
        cmd.Connection.Open();
        SqlDataReader ddlValues = default(SqlDataReader);
        ddlValues = cmd.ExecuteReader();
        //if (!IsPostBack) {
            ddlDrillDown.DataSource = ddlValues;
            ddlDrillDown.DataValueField = "content_id";
            ddlDrillDown.DataTextField = "content_title";
            ddlDrillDown.DataBind();
            //set the default value for the drop down
            ListItem Item = new ListItem();
            Item.Text = "Select a Physician's Name";
            Item.Value = "0";
            //Item.Selected = True
            ddlDrillDown.Items.Insert(0, Item);
        //}
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    }
    public void PopulateSpecialty() {
        SqlCommand cmd = new SqlCommand("getSpecialties", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
        cmd.Connection.Open();
        SqlDataReader ddlValues = default(SqlDataReader);
        ddlValues = cmd.ExecuteReader();
        //if (!IsPostBack) {
            ddlDrillDown.DataSource = ddlValues;
            ddlDrillDown.DataValueField = "content_id";
            ddlDrillDown.DataTextField = "content_title";
            ddlDrillDown.DataBind();
            //set the default value for the drop down
            ListItem Item = new ListItem();
            Item.Text = "Select a Specialty";
            Item.Value = "0";
            ddlDrillDown.Items.Insert(0, Item);
        //}
        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
    public void PopulateLocation() {
        SqlCommand cmd = new SqlCommand("getLocations", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
        cmd.Connection.Open();
        SqlDataReader ddlValues = default(SqlDataReader);
        ddlValues = cmd.ExecuteReader();
        //if (!IsPostBack) {
            ddlDrillDown.DataSource = ddlValues;
            ddlDrillDown.DataValueField = "content_id";
            ddlDrillDown.DataTextField = "content_title";
            ddlDrillDown.DataBind();
            //set the default value for the drop down
            ListItem Item = new ListItem();
            Item.Text = "Select a Location";
            Item.Value = "0";
            ddlDrillDown.Items.Insert(0, Item);
        cmd.Connection.Close();
        cmd.Connection.Dispose();
        //}
    }
    public void ddlMain_SelectedIndexChanged(object sender, System.EventArgs e) {
        switch(ddlMain.SelectedItem.Value) {
            case "0":
                PopulatePhysician();
                break;
            case "1":
                PopulateLocation();
                break;
            case "2":
                PopulateSpecialty();
                break;
        }
    }
}

当页面第一次加载时,PopulatePhysician();通过填充select选项工作得很好。但是当我调用SelectedIndexChanged()函数时,什么也没有发生。

如何解决?case表述正确吗?

为什么asp:DropDownList不响应代码隐藏

您还没有为SelectedIndexChanged事件添加任何委托。

OnSelectedIndexChanged添加到asp:DropDownList标签的参数中,如下所示:

<asp:DropDownList ClientIDMode="Static" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true"  >

编辑

我刚刚注意到你实际上错过了AutoPostBack事件-它应该设置为true,因为更新选择而不提交表单将不会触发回发事件,除非AutoPostBack设置为true

下拉列表中需要AutoPostback=true

<asp:DropDownList AutoPostback="true" ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">

从函数中删除If (!IsPostBack)子句。在页面加载中检查,但不要在每个单独的函数中检查。