开发者

Remove and insert drop down list values

I have a couple of dropdown lists like this

<asp:DropDownList runat="server" OnSelectedIndexChanged="Update_ddls" AutoPostBack="true" ID="ddl1" />

<asp:DropDownList runat="server" ID="ddl2" />

Which are populated by a column in my db like this

query.CommandText = "SELECT * FROM locations"

DataTable = New DataTable()
DataAdapter = New SqlDataAdapter(query)
DataAdapter.Fill(DataTable)

If Not Page.IsPostBack Then
    ddl1.DataSource = DataTable.DefaultView
    ddl1.DataTextField = "location"
    ddl1.DataValueField = "location"
    ddl1.DataBind()
    ddl1.Items.Insert(0, New ListItem("Select...", "Select..."))
End If

I also have a function called Update_d开发者_运维问答dls which runs OnSelectedIndexChanged that looks like this

ddl2.Items.Remove(ddl2.Items.FindByValue(ddl1.SelectedValue.ToString()))

Which removes the selected item in ddl1 from the second dropdown but if I keep changing the ddl1 item then ddl2 won't have any items left in it.

Is there a way to re-add the previously deleted items in the Update_dlls function without re-databinding because if I re-databind I lose the selected item in the ddl?

Thanks in advance


Store SelectedValue in some variable before rebinding the dropdownlist and then simply remove the value from dropdownlist


You could re-databind ddl2 in your OnSelectedIndexChanged handler for ddl1 and then remove the selectedValue from ddl1 after the fact.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜