简体   繁体   English

为什么asp:DropDownList不响应后台代码

[英]Why asp:DropDownList does not respond to code-behind

I have the following ASP drop-down: 我有以下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>

I am trying to make it interactive where if the first select option is changed the second will change as well based on the selection from the first select option. 我正在尝试使其具有交互性,如果更改了第一个选择选项,则第二个也将根据第一个选择选项的选择进行更改。

My C# code looks like this: 我的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;
        }
    }
}

When the page first loads, PopulatePhysician(); 首次加载页面时, PopulatePhysician(); works great by populating the select option. 通过填充选择选项,效果很好。 But when I call the SelectedIndexChanged() function, nothing happens. 但是,当我调用SelectedIndexChanged()函数时,什么也没发生。

How can I resolve it? 我该如何解决? Is the case statement correct? case陈述正确吗?

在下拉列表中需要AutoPostback=true

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

You haven't added any delegates to the SelectedIndexChanged event. 您尚未将任何委托添加到SelectedIndexChanged事件。

Add OnSelectedIndexChanged to the parameters in the asp:DropDownList tag, like so: OnSelectedIndexChanged添加到asp:DropDownList标记中的参数中,如下所示:

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

EDIT 编辑

I just noticed you're actually missing the AutoPostBack event as well - it should be set to true , as updating the selection without submitting the form will not fire a postback event, unless AutoPostBack is set to true. 我只是注意到您实际上实际上也缺少AutoPostBack事件-应该将其设置为true ,因为在不提交表单的情况下更新选择不会触发回发事件,除非AutoPostBack设置为true。

Remove the If (!IsPostBack) clause from your functions. 从函数中删除If(!IsPostBack)子句。 Check for that in page load, but not in each individual function. 检查页面加载中的内容,而不检查每个单独的功能中的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的DropdownList值没有从我的后台代码传递给ASP中的C#类? - Why is my DropdownList value not being passed from my code-behind to my C# class in asp? Asp.net 在代码隐藏中打开 DropDownList 控件 - Asp.net open DropDownList control in code-behind 如何使用SP查询Xml文档并从背后隐藏代码填充ASP DropDownList? - How to query an Xml Document using a SP and populate an ASP DropDownList from code-behind? 为什么在后台代码中将display设置为none不会将其添加到HTML? - Why does setting display to none in the code-behind not add it to the HTML? EntityDataSource既在ASP方面又在代码隐藏方面? - EntityDataSource both on ASP and code-behind side? 如何从后台代码更新DropDownList的SQLDataSource - How to update the SQLDataSource of a DropDownList from code-behind ASP.NET标记如何在代码隐藏中访问变量? - ASP.NET how does markup access to variables in code-behind? 为什么WPF绑定不调用PropertyMetadata中定义的propertyChangedCallback,而隐藏代码中的setter呢? - Why WPF Binding does not invoke propertyChangedCallback defined in PropertyMetadata, while setter in code-behind does? 为什么用&#39;runat =“ server”装饰HTML元素不能使它们从后面的代码访问? - Why does decorating HTML elements with 'runat=“server”' not make them accessible from the code-behind? 为什么javascript indexOf()方法在从C#代码隐藏调用时抛出错误? - Why does javascript indexOf() method throw error when called from C# code-behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM