I have implemented an asp.net listbox in my project. The listbox includes two buttons, one for adding items and the other for removing them. I have used a JavaScript function to handle item removal from the listbox. However, when I add new items after removing some, the previously removed items reappear in the list.
<asp:ListBox ID="sLstbox" runat="server" Width="250px" Height="150px" TabIndex="10"></asp:ListBox>
<asp:LinkButton ID="sLbtnAdd" runat="server" ></asp:LinkButton>
<a href="#" id="hAncRemove" runat="server" onclick="fncRemoveItems();">Remove</a>
function fncRemoveItems()
{
var i;
var strIDs="";
var items = document.getElementById("sLstbox");
alert(items.options.length);
for (i = items.options.length - 1; i >= 0; i--)
{
if (items.options[i].selected)
items.remove(i);
}
}
IN code
Protected Sub sLbtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sLbtnAdd.Click
Dim li As New ListItem
li.Value = "1"
li.Text = "test"
sLstbox.Items.Add(li)
End Sub