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

DreamTeK
  • 32,537
  • 27
  • 112
  • 171

5 Answers5

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 ...", ""))
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • 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. – John Waclawski Sep 18 '15 at 19:52
  • @JohnWaclawski if `AppendDataBoundItems="False"` then whenever data is bound to the list it will automatically clear the list beforehand. – DreamTeK Sep 21 '15 at 07:30
11

You should clear out your listbbox prior to binding:

 Me.ddl2.Items.Clear()
  ' now set datasource and bind
jason
  • 3,821
  • 10
  • 63
  • 120
6

Please use the following

ddlCity.Items.Clear();
Sheryar Nizar
  • 285
  • 4
  • 5
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();
}
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
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

Adam
  • 3,815
  • 29
  • 24
  • I really appreciate your response however I have no idea what =new List() is in VB. Its a new one on me. Do you know a VB example? – DreamTeK May 09 '13 at 15:11
  • see the Edit its equal to New List(Of String)() in Vb.net – Adam May 09 '13 at 16:09