Introduction: In this article I
will explain how to move items from one listbox to other listbox and vice versa
in asp.net
Explanation: I have added the two listboxes to webform. One listbox contain
the country name I will move data from one listbox to other listbox.
Design Section:
<fieldset style="width:300px">
<legend><strong>ListBox
Example</strong></legend>
<table style="height: 168px; width: 279px">
<tr><td> <asp:ListBox ID="lstbcountry" runat="server" SelectionMode="Multiple"
Height="102px" Width="99px">
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>England</asp:ListItem>
<asp:ListItem>Cuba</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
<asp:ListItem>Singapore</asp:ListItem>
</asp:ListBox></td>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text=">" onclick="Button1_Click" />
<br />
<asp:Button ID="Button3" runat="server" Text=">>" onclick="Button3_Click" />
<br />
<asp:Button ID="Button2"
runat="server" Text="<" onclick="Button2_Click" />
<br />
<asp:Button ID="Button4" runat="server" Text="<<" onclick="Button4_Click" />
</td>
<td><asp:ListBox ID="lstbcountry2" runat="server" SelectionMode="Multiple"
Height="96px" Width="97px">
</asp:ListBox></td></tr>
</table>
</fieldset>
ASP.NET Code (C#): write the following code in
code behind file.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial
class listbox
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
int i = 0;
protected void Button1_Click(object
sender, EventArgs e)
{
try
{
lstbcountry2.Items.Add(lstbcountry.SelectedItem);
i = lstbcountry.SelectedIndex;
lstbcountry.Items.RemoveAt(i);
}
catch (Exception ex)
{
}
}
protected void Button2_Click(object
sender, EventArgs e)
{
try
{
lstbcountry.Items.Add(lstbcountry2.SelectedItem);
i = lstbcountry2.SelectedIndex;
lstbcountry2.Items.RemoveAt(i);
}
catch (Exception ex)
{
}
}
protected void Button3_Click(object
sender, EventArgs e)
{
try
{
for
(i = 0; i <= lstbcountry.Items.Count - 1; i++)
{
lstbcountry2.Items.Add(lstbcountry.Items[i]);
}
lstbcountry.Items.Clear();
}
catch (Exception ex)
{
}
}
protected void Button4_Click(object
sender, EventArgs e)
{
try
{
for
(i = 0; i <= lstbcountry2.Items.Count - 1; i++)
{
lstbcountry.Items.Add(lstbcountry2.Items[i]);
}
lstbcountry2.Items.Clear();
}
catch (Exception ex)
{
}
}
}
VB code:
Dim i As Integer = 0
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesButton2.Click
lstbcountry.Items.Add(lstbcity.SelectedItem)
i = lstbcountry2.SelectedIndex
lstbcountry2.Items.RemoveAt(i)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesButton1.Click
lstbcountry2.Items.Add(lstbcountry.SelectedItem)
i =
lstbcountry.SelectedIndex
lstbcountry.Items.RemoveAt(i)
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesButton3.Click
Try
For i = 0 To lstbcountry.Items.Count - 1
lstbcountry2.Items.Add(lstbcountry.Items(i))
Next
lstbcountry.Items.Clear()
Catch ex As Exception
End Try
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesButton4.Click
Try
For i = 0 To lstbcity.Items.Count - 1
lstbcountry.Items.Add(lstbcity.Items(i))
Next
lstbcountry2.Items.Clear()
Catch ex As Exception
End Try
End Sub
0 comments:
Post a Comment