I have two DropDownList controls in cascade (the second one is populated based on the selection made on the first one):
<asp:DropDownList ID="ddlProduct" runat="server" AppendDataBoundItems="true"
AutoPostBack="true" onselectedindexchanged="ddlProduct_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AppendDataBoundItems="true">
<asp:ListItem Value="" Selected="True"> - Category - </asp:ListItem>
</asp:DropDownList>
In the ddlProduct.SelectedIndexChanged event handler I have the following code:
ddlCategory.DataSource = _productService.GetCategoryByProductId(ddlProduct.SelectedValue);
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryId";
ddlCategory.DataBind();
The first time a user choose a Product from ddlProduct the ddlCategory is populated correctly. The following times, the Categories in ddlCategory are appended to those that were selected the first time and so on. I tried to put in ddlProduct.SelectedIndexChanged:
ddlCategory.Items.Clear();
but the method removes also the hardcoded item
<asp:ListItem Value="" Selected="True"> - Category - </asp:ListItem>
How can I just delete the appended item from ddlCategory?
You could set
AppendDataboundItems="false"and insert the default item manually, for example inDropDownList.DataBoundevent: