locked
DropDownList_SelectedIndexChanged() doesn't work RRS feed

  • Question

  • User2074777333 posted

    Hi,

    I have this code, and I connected the SelectedIndexChanged event to the DropDownList but on selecting an item it doesn't do anything:

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
              
                String Ime = DropDownList1.SelectedItem.Value.Trim();
                
    
                SqlCommand inSqlCommand =
                        new SqlCommand("listIncidents", conn);
                inSqlCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter inParameter = new SqlParameter
                    ("@Iname", SqlDbType.NVarChar, 60);
                inParameter.Direction = ParameterDirection.Input;
                inParameter.Value = Ime;
                inSqlCommand.Parameters.Add(inParameter);
    
                SqlDataReader inSqlDataReader;
                try
                {
                    conn.Open();
                    inSqlDataReader = inSqlCommand.ExecuteReader();
    
                    while (inSqlDataReader.Read())
                    {
                        txtName.Text = inSqlDataReader[0].ToString();
                        txtLattitude.Text = Convert.ToString(inSqlDataReader[2]);
                        txtLongitude.Text = Convert.ToString(inSqlDataReader[1]);
                    }
                    inSqlDataReader.Close();
                }
                catch (SqlException exc)
                {
                    Response.Write(exc.ToString());
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
                finally
                {
                    conn.Close();
                }
            }

    Can anybody help me what might be wrong?

    Thanks

    Wednesday, April 9, 2014 10:42 AM

Answers

  • User-760709272 posted

    The event fires when the form is submitted (ie the user clicks a submit button).  If you want the event to fire right away then add the

    AutoPostback="True"

    attribute to your droplist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:48 AM
  • User2103319870 posted

    Have you set DropDownList AutoPostBack property to true.if not please set it.

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:48 AM

All replies

  • User-760709272 posted

    The event fires when the form is submitted (ie the user clicks a submit button).  If you want the event to fire right away then add the

    AutoPostback="True"

    attribute to your droplist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:48 AM
  • User2103319870 posted

    Have you set DropDownList AutoPostBack property to true.if not please set it.

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:48 AM
  • User-1716253493 posted
    Make sure you populate DropDownList items inside if(!IsPostBack) block
    Wednesday, April 9, 2014 11:00 AM
  • User2074777333 posted

    Thanks to all of you

    Wednesday, April 9, 2014 11:05 AM