Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear exisiting dropdownlist items when its content changes?

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

like image 243
DreamTeK Avatar asked May 09 '13 11:05

DreamTeK


People also ask

How to clear drop down list in vb net?

ClearSelection() did exactly what I needed it to do. It cleared the dropdownlist selection so that nothing was selected. The box was blank, but when you dropdown the list, all the options are still available to choose from.

How do I remove a selected value from a DropDownList?

With jQuery, you can use the . remove() method to takes elements out of the DOM. To get the selected item from a dropdown, you can use the :selected property and call remove() on the matched element.

How do I add a dynamic item to a drop down list?

To add a dropdown list dynamically, you would need to create the HTML <select> element, its label and optionally a <br> tag. In pure JavaScript, you can use the document. createElement() method to programmatically create a dropdown list. Then you can call the Node's appendChild() method or jQuery's .


2 Answers

You should clear out your listbbox prior to binding:

 Me.ddl2.Items.Clear()
  ' now set datasource and bind
like image 145
jason Avatar answered Oct 10 '22 20:10

jason


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 ...", ""))
like image 21
DreamTeK Avatar answered Oct 10 '22 21:10

DreamTeK