c# - 为什么asp :DropDownList does not respond to code-behind

标签 c# asp.net

我有以下 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(); 会填充选择选项,效果很好。但是当我调用 SelectedIndexChanged() 函数时,没有任何反应。

我该如何解决? case 语句是否正确?

最佳答案

您尚未向 SelectedIndexChanged 事件添加任何委托(delegate)。

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。

关于c# - 为什么asp :DropDownList does not respond to code-behind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23664969/

相关文章:

c# - 如何摆脱警告 "Bot Framework State API is not recommended for production environments, and may be deprecated in a future release."

c# - 将值附加到列表

c# - Windows 反恶意软件扫描接口(interface) - ASP.NET/IIS

c# - HtmlGenericControl 和 id

c# - SqlLocalDb 中的服务代理或 SqlDependency?

c# - 该方法或操作未实现。在 C# 中停止 IIS 网站时

c# - 我怎样才能得到按钮的命令参数?

c# - 在 Aspx 中分配 C# Lambda Func

c# - 当文本框值为空时使用 C# 停止在表中插入空值日期

asp.net - 有谁知道如何摆脱来 self 的 .net Web 服务的 jsdebug 请求