asp.net - How to clear exisiting dropdownlist items when its content changes? -
ddl2 populates based on ddl1 selected value successfully.
my issue data present in ddl2 not clear before appending new data ddl2 content continues grow every time ddl1 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 , items.clear little success.
protected sub ddl1_selectedindexchanged(byval sender object, byval e eventargs) ddl2.items.clear() ddl2.datasource = sql2 ddl2.databind() end sub
question
how items present in asp:dropdownlist clear before new values populated when dropdownlists content dependent on dropdownlists selected value?
please post code in vb
using ddl.items.clear()
clear dropdownlist must sure dropdownlist not set to:
appenddatabounditems="true"
this option cause rebound data appended existing list not cleared prior binding.
solution
add appenddatabounditems="false"
dropdownlist.
now when data rebound automatically clear existing data beforehand.
protected sub ddl1_selectedindexchanged(sender object, e eventargs) ddl2.datasource = sql2 ddl2.databind() end sub
note: this may not suitable in situations appenddatbound items can cause dropdown append own data on each change of list.
top tip
still want default list item adding dropdown need rebind data?
use appenddatabounditems="false"
prevent duplication data on postback , directly after binding dropdownlist insert new default list item.
ddl.items.insert(0, new listitem("select ...", ""))
Comments
Post a Comment