copying from one list box to another list box

Posted by Sanoop under ASP.NET on 11/19/2011 | Points: 10 | Views : 4368 | Status : [Member] | Replies : 2
Hi friends
Can anyone send me the codes for copying selected items from one list box to another. I'm asking codes by using tool bar not the server side coding.




Responses

Posted by: Chanti444 on: 11/19/2011 [Member] Starter | Points: 25

Up
0
Down
asp:code

<div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
</div>

satya

Sanoop, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chanti444 on: 11/19/2011 [Member] Starter | Points: 25

Up
0
Down
source code:
protected void Button1_Click(object sender, EventArgs e)
{
string ItemsAlreadyExists = "";
for (int i = 0; i <= ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!ListBox2.Items.Contains(ListBox1.Items[i]))
{
ListBox2.Items.Add(ListBox1.Items[i]);
}
else
{
ItemsAlreadyExists += Convert.ToString(ListBox1.Items[i].Text) + ",";
}
}
}
if (ItemsAlreadyExists != "")
{
Response.Write(ItemsAlreadyExists.TrimEnd(',') + " Already exists in the ListBox");
}
ListBox2.ClearSelection();
}

satya

Sanoop, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response