Move listbox Items In Asp.net from one to Another
Move listbox Items In Asp.net from one to Another
Step 1 : Design .ASPX page
Here In Design part ,I
am define two Listbox and two Button for left and right.
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" Width="150px" Height="200px" SelectionMode="Multiple"></asp:ListBox>
</td>
<td>
<asp:Button ID="btnryt" runat="server" Text="Move Reight" OnClick="btnryt_Click" />
<asp:Button ID="btnLeft" runat="server" Text="Move Left" OnClick="btnLeft_Click" />
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" Width="150px" Height="200px" SelectionMode="Multiple"></asp:ListBox>
</td>
</tr>
</table>
</div>
</form>
Step 2 : Logic Part
if (!Page.IsPostBack)
{
//Bind
Some Items in ListBox1
ListBox1.Items.Add("Kandy");
ListBox1.Items.Add("Deol");
ListBox1.Items.Add("Sam");
ListBox1.Items.Add("Rahul");
ListBox1.Items.Add("Joytismoy");
ListBox1.Items.Add("HAnsa");
}
//Logic
for Move Items from Left to right.
protected void btnryt_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListItem li = ListBox1.Items[i];
ListBox1.Items.Remove(li);
}
}
}
//Logic
for Move Items from right to Left.
protected void btnLeft_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListItem li = ListBox2.Items[i];
ListBox2.Items.Remove(li);
}
}
}
Comments
Post a Comment