简体   繁体   English

DropDownList OnSelectedIndexChanged未触发(AutoPostBack为“ true”,未发现其他问题)

[英]DropDownList OnSelectedIndexChanged not firing (AutoPostBack is “true”, no other issues found)

ListBox1 connects to SQL database and binds the queried data to ddCountries (the DropDownList). ListBox1连接到SQL数据库,并将查询的数据绑定到ddCountries(DropDownList)。 When a DropDownList item is selected, it's supposed to update a label elsewhere on the page however for some reason the ddCountries_SelectedIndexChanged method isn't being accessed at all when the app is running. 选择DropDownList项目时,应该更新页面上其他位置的标签,但是由于某些原因,在应用程序运行时根本无法访问ddCountries_SelectedIndexChanged方法。

Yes, AutoPostBack is set to "true". 是的,AutoPostBack设置为“ true”。

Default.aspx Default.aspx

ListBox1: ListBox1:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection countriesConnection = new SqlConnection();

            countriesConnection.ConnectionString =
                System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CSharpClass1ConnectionString"].ConnectionString;

            SqlCommand cmd = countriesConnection.CreateCommand();

            int ContID = Convert.ToInt32(ListBox1.SelectedValue);

            try
            {
                string query = "SELECT * FROM Country WHERE ContinentId=" + ContID + ";";

                SqlDataAdapter adpt = new SqlDataAdapter(query, countriesConnection);
                DataTable dt = new DataTable();
                adpt.Fill(dt);
                ddCountries.DataSource = dt;
                ddCountries.DataBind();
                ddCountries.DataTextField = "CountryName";
                ddCountries.DataValueField = "ContinentId";
                ddCountries.DataBind();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                cmd.Dispose();
                countriesConnection.Close();
            }
        }

DropDownList: 下拉列表:

<asp:DropDownList ID="ddCountries" runat="server" Height="16px" Width="238px" OnSelectedIndexChanged="ddCountries_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Text="None" value=""></asp:ListItem>
    </asp:DropDownList>

Default.aspx.cs Default.aspx.cs

protected void ddCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblThankYou.Visible = true;
            lblThankYou.Text = "You have selected " + ddCountries.SelectedValue.ToString() + "!";
        }

There are no error messages, the label (lblThankYou) simply never gets updated. 没有错误消息,标签(lblThankYou)根本不会被更新。 According to debugging the method never gets accessed. 根据调试,该方法永远不会被访问。

You should use UpdatePanle and ScriptManager : 您应该使用UpdatePanle和ScriptManager:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true" >      
  <ContentTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem>item 1</asp:ListItem>
      <asp:ListItem>item 2</asp:ListItem>
    </asp:DropDownList>
  </ContentTemplate>
</asp:UpdatePanel>



protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM