The things I learnt while I migrated from classic asp to asp.net is given here for my reference as well as for new beginners.

LISTBOX LESSON 2 HOW TO MOVE DATA FROM LISTBOX TO ANOTHER

ListBox Lesson 2

Today we are going to learn how to Move the Data From One ListBox to Another?

 

Step 1: Drag and Drop Two ListBoxes,Three Buttons and One TextBox. Right Click on the ListBox1 and ListBox2 and set the property "SelectionMode=Multiple" as shown here:

Step 2: Properly modify the Code of aspx and aspx.cs files as shown here:

Code for ListBoxMove.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBoxMove.aspx.cs" Inherits="ListBoxMove" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table >
<tr>
<td >
<asp:ListBox ID="ListBox1" runat="server"
SelectionMode="Multiple"
style="font-size:25px" Width="100px">
</asp:ListBox>
</td>
<td >
<asp:Button
ID="bnMoveRight"
runat="server"
Text="&gt;&gt;"
onclick="bnMoveRight_Click" />
<br />
<asp:Button
AccessKeyID="bnMoveLeft"
runat="server"
Text="&lt;&lt;"
onclick="bnMoveLeft_Click" />
</td>
<td>
<asp:ListBox
ID="ListBox2"
runat="server"
SelectionMode="Multiple"
style="font-size:25px" onselectedindexchanged="ListBox2_SelectedIndexChanged"
Width="100px">
</asp:ListBox>
</td>
</tr>
</table>

</div>
<asp:TextBox ID="TextBox1"
runat="server">
</asp:TextBox>
<asp:Button
ID="bnInsert"
runat="server"
onclick="bnInsert_Click"
Text="Insert into Box1" />
</form>
</body>
</html>

Code for ListBoxMove.aspx.cs

Note: When you double click on the Button, some of the codes such as "protected void........." will be automatically created. You have to type the inner content only.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListBoxMove : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void bnMoveRight_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.RemoveAt(i);
}
}

}


protected void bnMoveLeft_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.RemoveAt(i);
}
}
}
protected void bnInsert_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(TextBox1.Text);
TextBox1.Text = "";
}

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}
}


Step 2: In the Solution Explorer, Right click on our aspx file and select "View in Browser" :

Step 3: You will get the output like this in Browser:

Step 4: Try to insert some data into Listbox1 as shown here:

Step 5: Click on the Move Button, to move the data from One ListBox to another.

ASP.NET Video Tutorial : How to Move Data from One ListBox to Another

Thanks for Your Visit

Google Search
Disclaimer and Copy Right