简体   繁体   中英

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). 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.

Yes, AutoPostBack is set to "true".

Default.aspx

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

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. According to debugging the method never gets accessed.

You should use UpdatePanle and 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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