Search code examples
c#asp.netpostbackautopostback

post back dropdownlist


on this site when dropdownlist contains only one item, when clicked it doesn't cause a post back

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.Items.Add("a");

        DropDownList2.Items.Add("a");
        DropDownList2.Items.Add("b");

    }
}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write(DropDownList1.Text);//does not work ????????????
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write(DropDownList2.Text);

}

on this site


Solution

  • DropDownList1_SelectedIndexChanged will never trigger because you only have 1 item in DropDownList1, thus index will never be changed.

    Updated

    What you can do is add a null value to the dropdownlist1, like so

    <asp:DropDownList runat="server" ID="DropDownList1">
        <asp:ListItem Value="0" Text="Choose option" Selected="true" />
        <asp:ListItem Value="1" Text="a" />
    </asp:DropDownList>