locked
DropdownList SelectedIndexChanged Always Returns Index 3 RRS feed

  • Question

  • User1769015664 posted

    Why do I always get the selectedIndex 3 when I select 5, 10 , 15 etc.?

    <asp:DropDownList ID="ddlCourses" runat="server" OnSelectedIndexChanged="ddlCourses_SelectedIndexChanged" ></asp:DropDownList>
    int n = ddlCourses.SelectedIndex;



    Wednesday, May 31, 2017 2:39 AM

All replies

  • User-1838255255 posted

    Hi NJ2,

    According to your code, I make a test in my side, I think you need set the AutoPostBack value to true. This is the work sample, please check:

    <asp:DropDownList ID="ddlCourses" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCourses_SelectedIndexChanged">
                    <asp:ListItem>111</asp:ListItem>
                    <asp:ListItem>222</asp:ListItem>
                    <asp:ListItem>333</asp:ListItem>
    </asp:DropDownList>
    
    
    protected void ddlCourses_SelectedIndexChanged(object sender, EventArgs e)
            {
                int n = ddlCourses.SelectedIndex;
            }

    Also I am not clear where you get index 3, I hope you could post the complete code for us to check!

    Best regards,

    Eric Du

    Wednesday, May 31, 2017 10:15 AM
  • User1769015664 posted

    I did not want the AutoPostBack; got it to work using Updatepanel.

    Thursday, June 1, 2017 7:42 AM
  • User-1838255255 posted

    Hi NJ2,

    Through your description, I suggest you check where you bind the dropdownlist and check if set the default value.

    Also I hope you could post the complete sample code for us to test!

    Best regards,

    Eric Du

    Thursday, June 1, 2017 8:56 AM