How to clear existing elements of a drop-down list when changing its contents?

ddl2 is successfully populated based on the selected ddl1 value.

My problem is that data that is already present in ddl2 is not cleared until new data is added, so the contents of ddl2 continues to grow every time ddl1 changes.

<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 tried to rebuild the code in the code behind the selected index change as well as 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 the elements present in the asp: drop drop-down list to clear before new values ​​are filled when the contents of dropdownlists depend on another dropdownlists value?

Please put any code in VB

+9
source share
5 answers

Using ddl.Items.Clear() will clear the drop-down list, however you must be sure that your drop-down list is not configured to:

 AppendDataBoundItems="True" 

This option will add the bounce data to the existing list, which will NOT be cleared before binding.

Decision

Add AppendDataBoundItems="False" to the drop-down list.

Now that the data is being restored, it will automatically clear all existing data.

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

NOTE. . This may not be acceptable in all situations, since appenddatbound elements can cause the drop-down list to add its own data each time the list changes.


TOP TIP

Still want the default list item to add to the drop-down list, but do you need to reset the data?

Use AppendDataBoundItems="False" to prevent data duplication during AppendDataBoundItems="False" , and then immediately after binding your drop-down list, insert a new default list item.

 ddl.Items.Insert(0, New ListItem("Select ...", "")) 
+20
source

You must clear your bbox list before binding:

  Me.ddl2.Items.Clear() ' now set datasource and bind 
+9
source

Please use the following

 ddlCity.Items.Clear(); 
+5
source

Just 2 easy steps to solve your problem.

First, check the AppendDataBoundItems property and set it to false.

Second, clear all elements using the .clear () property

 { ddl1.Items.Clear(); ddl1.datasource = sql1; ddl1.DataBind(); } 
+2
source

just compiled your code, and the only thing missing from it is that you need to bind your ddl2 to an empty data source before you bind it like this:

Protected Sub ddl1_SelectedIndexChanged (ByVal sender as object, ByVal e As EventArgs) // ddl 2.Items.Clear ()

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

and it worked just fine

+1
source

Source: https://habr.com/ru/post/944623/


All Articles