This article will show you how you can move listbox item
from one listbox to another listbox using c#.net in asp.net.
Some of my previous articles are as follows: jQuery
Ajax Duplicate UserId Validation in Asp.Net Without Page Refresh Using C#.Net,
Ajax
Login Form Validation Without Page Refresh Using jQuery In Asp.Net and C#.Net,
Validate
DropDownlist Selected Value Using jQuery In Asp.net ,C#.Net, Single
RadioButton Selection in GridView In Asp.Net Using C#.Net, Bind
and Validate GridView TextBox Value by jQuery In Asp.Net Using C#, Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#.
So for this article first we will create a new asp.net
application and add below code into the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="WebApplication1.WebForm6"
%>
<!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>Move
ListBox Items From one ListBox To another ListBox Using C#.Net In Asp.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%">
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" Height="105px" Width="118px"
SelectionMode="Multiple">
<asp:ListItem>List1
Item 1</asp:ListItem>
<asp:ListItem>List1
Item2</asp:ListItem>
<asp:ListItem>List1
Item 3</asp:ListItem>
<asp:ListItem>List1
Item 4</asp:ListItem>
<asp:ListItem>List1
Item 5</asp:ListItem>
</asp:ListBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text=">>"
onclick="Button1_Click" /><br />
<asp:Button ID="Button2" runat="server" Text="<<"
onclick="Button2_Click" />
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" Height="105px" Width="118px"
SelectionMode="Multiple">
<asp:ListItem>List2
Item 1</asp:ListItem>
<asp:ListItem>List2
Item2</asp:ListItem>
<asp:ListItem>List2
Item 3</asp:ListItem>
<asp:ListItem>List2
Item 4</asp:ListItem>
<asp:ListItem>List2
Item 5</asp:ListItem>
</asp:ListBox>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above code when we move item from one list box to another
listbox post back will take place.
So here is the code to move the one list box item to another
listbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm6 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
if
(ListBox1.SelectedIndex != -1)
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.Remove(ListBox1.SelectedItem);
}
}
protected
void Button2_Click(object
sender, EventArgs e)
{
if
(ListBox2.SelectedIndex != -1)
{
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}
}
}
|
Now we have done run the application and check the output.
0 comments:
Please let me know your view