How to clear exisiting dropdownlist items when its content changes?
Asked Answered
M

5

12

ddl2 populates based on ddl1 selected value successfully.

My issue is the data that is already present in ddl2 does not clear before appending the new data so ddl2 content just continues to grow every time ddl1 is changed.

<asp:DropDownList ID="ddl1" RunAt="Server" DataSourceID="sql1" DataValueField="ID1" DataTextField="Name2" AppendDataBoundItems="True" AutoPostBack="True">
  <asp:ListItem Text="ALL" Selected="True" Value="0"/>
</asp:DropDownList>

<asp:DropDownList ID="ddl2" RunAt="Server" DataSourceID="sql2" DataValueField="ID2" DataTextField="Name2" AppendDataBoundItems="True" AutoPostBack="True">
  <asp:ListItem Text="ALL" Selected="True" Value="0"/>
</asp:DropDownList>

<asp:SqlDataSource ID="sql1" RunAt="Server" SelectCommand="sp1" SelectCommandType="StoredProcedure"/>

<asp:SqlDataSource ID="sql2" RunAt="Server" SelectCommand="sp2" SelectCommandType="StoredProcedure">
  <SelectParameters>
    <asp:ControlParameter Type="Int32" Name="ID1" ControlID="ddl1" PropertyName="SelectedValue"/>
  </SelectParameters>
</asp:SqlDataSource>

I have tried re-databinding in code behind on selected index change and also items.clear with little success.

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    ddl2.Items.Clear()
    ddl2.DataSource = sql2
    ddl2.DataBind()
End Sub

QUESTION

How to get items present in an asp:dropdownlist to clear before new values are populated when the dropdownlists content is dependent on another dropdownlists selected value?

Please post any code in VB

Mound answered 9/5, 2013 at 11:38 Comment(0)
M
27

Using ddl.Items.Clear() will clear the dropdownlist however you must be sure that your dropdownlist is not set to:

AppendDataBoundItems="True"

This option will cause the rebound data to be appended to the existing list which will NOT be cleared prior to binding.

SOLUTION

Add AppendDataBoundItems="False" to your dropdownlist.

Now when data is rebound it will automatically clear all existing data beforehand.

Protected Sub ddl1_SelectedIndexChanged(sender As Object, e As EventArgs)
    ddl2.DataSource = sql2
    ddl2.DataBind()
End Sub

NOTE: This may not be suitable in all situations as appenddatbound items can cause your dropdown to append its own data on each change of the list.


TOP TIP

Still want a default list item adding to your dropdown but need to rebind data?

Use AppendDataBoundItems="False" to prevent duplication data on postback and then directly after binding your dropdownlist insert a new default list item.

ddl.Items.Insert(0, New ListItem("Select ...", ""))
Mound answered 22/5, 2014 at 7:52 Comment(2)
Although this cleared out my selected value, it also cleared out all my drop down values to make them not available again. I tried both "True" and "False" on the AppendDataBoundItems. I'm sure it's probably something I missed but wanted to throw this out there just in case.Falsify
@JohnWaclawski if AppendDataBoundItems="False" then whenever data is bound to the list it will automatically clear the list beforehand.Mound
F
11

You should clear out your listbbox prior to binding:

 Me.ddl2.Items.Clear()
  ' now set datasource and bind
Fredericfrederica answered 9/5, 2013 at 11:48 Comment(1)
See my edits above. I am already doing this but it does not work.Mound
M
6

Please use the following

ddlCity.Items.Clear();
Mellicent answered 8/4, 2014 at 10:31 Comment(0)
N
2

Just 2 simple steps to solve your issue

First of all check AppendDataBoundItems property and make it assign false

Secondly clear all the items using property .clear()

{
ddl1.Items.Clear();
ddl1.datasource = sql1;
ddl1.DataBind();
}
Northamptonshire answered 6/10, 2015 at 6:35 Comment(0)
P
1

just compiled your code and the only thing that is missing from it is that you have to Bind your ddl2 to an empty datasource before binding it again like this:

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) //ddl2.Items.Clear()

ddl2.DataSource=New List(Of String)()
ddl2.DataSource = sql2
ddl2.DataBind() End Sub

and it worked just fine

Penicillium answered 9/5, 2013 at 14:47 Comment(2)
I really appreciate your response however I have no idea what =new List<string>() is in VB. Its a new one on me. Do you know a VB example?Mound
see the Edit its equal to New List(Of String)() in Vb.netPenicillium

© 2022 - 2024 — McMap. All rights reserved.