I have two Listbox controls,among one contains list of items when i select one item in listbox1 that item ll be added to listbox2.If I select the same item again it ll give a message like item already exists and that same item should not appear in my listbox2.
I did this, but its (duplicate item ) including in listbox2
anigova
Member
6 Points
19 Posts
repeated item should not be added to the listbox
Jan 10, 2013 08:52 AM|LINK
Hi
I have two Listbox controls,among one contains list of items when i select one item in listbox1 that item ll be added to listbox2.If I select the same item again it ll give a message like item already exists and that same item should not appear in my listbox2.
I did this, but its (duplicate item ) including in listbox2
ManikandanUl...
Participant
850 Points
253 Posts
Re: repeated item should not be added to the listbox
Jan 10, 2013 09:02 AM|LINK
pls refer this below code. Its working fine.
protected void btnMoveRight_Click(object sender, EventArgs e)
{
try
{
ArrayList txnMoved = new ArrayList();
if (string.IsNullOrEmpty(lstTxnLeft.SelectedValue))
{
lblErrMsg.Visible = true;
lblErrMsg.Text = "Select any Transaction to MoveRight";
}
else
{
lblErrMsg.Visible = false;
for (int i = lstTxnLeft.Items.Count - 1; i >= 0; i--)
{
if (lstTxnLeft.Items[i].Selected == true)
{
lstTxnRight.Items.Add(lstTxnLeft.Items[i]);
ListItem li = lstTxnLeft.Items[i];
string txnMval = lstTxnLeft.Items[i].Value.ToString();
lstTxnLeft.Items.Remove(li);
if (Session["delTxn"] != null)
{
txnMoved = (ArrayList)Session["delTxn"];
if (txnMoved.Contains(txnMval))
{
txnMoved.Remove(txnMval);
Session["delTxn"] = txnMoved;
}
}
}
}
}
}
catch (Exception ex)
{
log.ErrorFormat("Txns_MoveRight ended with exception:", ex);
}
}
protected void btnMoveLeft_Click(object sender, EventArgs e)
{
try
{
ArrayList delTxn1 = new ArrayList();
if (string.IsNullOrEmpty(lstTxnRight.SelectedValue))
{
lblErrMsg.Visible = true;
lblErrMsg.Text = "Select any Transaction to MoveLeft";
}
else
{
lblErrMsg.Visible = false;
for (int i = lstTxnRight.Items.Count - 1; i >= 0; i--)
{
if (lstTxnRight.Items[i].Selected == true)
{
lstTxnLeft.Items.Add(lstTxnRight.Items[i]);
ListItem li = lstTxnRight.Items[i];
string deleteTxns = lstTxnRight.Items[i].Value.ToString();
lstTxnRight.Items.Remove(li);
if (Session["delTxn"] != null)
{
delTxn1 = (ArrayList)Session["delTxn"];
}
delTxn1.Add(deleteTxns);
Session["delTxn"] = delTxn1;
}
}
}
}
catch (Exception ex)
{
log.ErrorFormat("Txns_MoveLeft ended with exception:", ex);
}
}
Click "…Mark As Answer" if my reply helpful....
urenjoy
Star
11997 Points
1797 Posts
Re: repeated item should not be added to the listbox
Jan 10, 2013 09:04 AM|LINK
Try this...
http://geekswithblogs.net/dotNETvinz/archive/2009/02/24/moving-listitems-between-two-listbox--server-side-approach.aspx
chandrasheka...
Star
10258 Points
1760 Posts
Re: repeated item should not be added to the listbox
Jan 10, 2013 09:15 AM|LINK
Hi
Try the below:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" SelectionMode="Multiple" OnSelectedIndexChanged="ListBox1_SelectionChanged"> <asp:ListItem Text="A" Value="1"></asp:ListItem> <asp:ListItem Text="B" Value="2"></asp:ListItem> <asp:ListItem Text="C" Value="3"></asp:ListItem> <asp:ListItem Text="D" Value="4"></asp:ListItem> </asp:ListBox> <asp:ListBox ID="ListBox2" runat="server"> <asp:ListItem Text="Select" Value="-1"></asp:ListItem> </asp:ListBox> <br /> <asp:Label ID="lblError" runat="server" ForeColor="Red" Text="Item already in the list. Please select a new one." Visible="false"></asp:Label>Code:
protected void ListBox1_SelectionChanged(object sender, EventArgs e) { if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null) { lblError.Visible = false; ListBox2.ClearSelection(); ListBox2.Items.Add(ListBox1.SelectedItem); } else { lblError.Visible = true; } }Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
anigova
Member
6 Points
19 Posts
Re: repeated item should not be added to the listbox
Jan 10, 2013 10:51 AM|LINK
@chandrashekar: Thank u..its working now.......