Nov 23, 2014

Posted in , , , ,

ASP. NET 4.5.2 Hosting - HostForLIFE.eu : Move Items from One ListBox to Another ListBox working with C#/VB. NET

With this article, I will be able to make a case for concerning ASP.NET Move Items from One ListBox to Another ListBox. And this is the code that I used in ASP.NET:
http://hostforlife.eu/European-ASPNET-452-Hosting
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Listbox Sample</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<table align="center">
<tr>
<td align="center" >
<b>Courses</b>
</td>
<td></td>
<td>
<b>Selected Courses</b></td>
</tr>
<tr>
<td>
<asp:ListBox ID="lstCourses" runat="server" Height="175px" Width="120px" SelectionMode="Multiple">
<asp:listitem Value="0">Asp.Net</asp:listitem>
<asp:listitem Value="1">C#.Net</asp:listitem>
<asp:listitem Value="2">VB.Net</asp:listitem>
<asp:listitem Value="3">JavaScript</asp:listitem>
<asp:listitem Value="4">Ajax</asp:listitem>
<asp:listitem Value="5">JQuery</asp:listitem>
</asp:ListBox>
</td>
<td>
<table>
<tr>
<td>
<asp:Button ID="btnMoveRight" runat="server" Text=">" Width="45px" onclick="btnMoveRight_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnMoveLeft" runat="server" Text="<" Width="45px" onclick="btnMoveLeft_Click" />
</td>
</tr>
</table>
</td>
<td>
<asp:ListBox ID="lstSelected" runat="server" Height="175px" Width="120px" SelectionMode="Multiple">
</asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class ListBox : System.Web.UI.Page
int student_id = 009;
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void btnMoveRight_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (lstCourses.SelectedIndex >= 0)
{
for (int i = 0; i < lstCourses.Items.Count; i++)
{
if (lstCourses.Items[i].Selected)
{
if (!arraylist1.Contains(lstCourses.Items[i]))
{
arraylist1.Add(lstCourses.Items[i]);
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!lstSelected.Items.Contains(((ListItem)arraylist1[i])))
{
lstSelected.Items.Add(((ListItem)arraylist1[i]));
}
lstCourses.Items.Remove(((ListItem)arraylist1[i]));
}
lstSelected.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in lstCourses to move";
}
}
protected void btnMoveLeft_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (lstSelected.SelectedIndex >= 0)
{
for (int i = 0; i < lstSelected.Items.Count; i++)
{
if (lstSelected.Items[i].Selected)
{
if (!arraylist2.Contains(lstSelected.Items[i]))
{
arraylist2.Add(lstSelected.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!lstCourses.Items.Contains(((ListItem)arraylist2[i])))
{
lstCourses.Items.Add(((ListItem)arraylist2[i]));
}
lstSelected.Items.Remove(((ListItem)arraylist2[i]));
}
lstCourses.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in lstSelected to move";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string s = string.Empty;
for (int i = 0; i < lstSelected.Items.Count; i++)
{
s=s+ lstSelected.Items[i].ToString();
}
}
}

In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Partial Public Class ListBox
Inherits System.Web.UI.Page
Private student_id As Integer = 9
Private arraylist1 As New ArrayList()
Private arraylist2 As New ArrayList()
Protected Sub btnMoveRight_Click(ByVal sender As Object, ByVal e As EventArgs)
lbltxt.Visible = False
If lstCourses.SelectedIndex >= 0 Then
For i As Integer = 0 To lstCourses.Items.Count – 1
If lstCourses.Items(i).Selected Then
If Not arraylist1.Contains(lstCourses.Items(i)) Then
arraylist1.Add(lstCourses.Items(i))
End If
End If
Next
For i As Integer = 0 To arraylist1.Count – 1
If Not lstSelected.Items.Contains(DirectCast(arraylist1(i), ListItem)) Then
lstSelected.Items.Add(DirectCast(arraylist1(i), ListItem))
End If
lstCourses.Items.Remove(DirectCast(arraylist1(i), ListItem))
Next
lstSelected.SelectedIndex = -1
Else
lbltxt.Visible = True
lbltxt.Text = "Please select atleast one in lstCourses to move"
End If
End Sub
Protected Sub btnMoveLeft_Click(ByVal sender As Object, ByVal e As EventArgs)
lbltxt.Visible = False
If lstSelected.SelectedIndex >= 0 Then
For i As Integer = 0 To lstSelected.Items.Count – 1
If lstSelected.Items(i).Selected Then
If Not arraylist2.Contains(lstSelected.Items(i)) Then
arraylist2.Add(lstSelected.Items(i))
End If
End If
Next
For i As Integer = 0 To arraylist2.Count – 1
If Not lstCourses.Items.Contains(DirectCast(arraylist2(i), ListItem)) Then
lstCourses.Items.Add(DirectCast(arraylist2(i), ListItem))
End If
lstSelected.Items.Remove(DirectCast(arraylist2(i), ListItem))
Next
lstCourses.SelectedIndex = -1
Else
lbltxt.Visible = True
lbltxt.Text = "Please select atleast one in lstSelected to move"
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim s As String = String.Empty

For i As Integer = 0 To lstSelected.Items.Count – 1
s = s & lstSelected.Items(i).ToString()
Next
End Sub
End Class


The output from the above method as shown inside the under picture. Whenever you choose the item in left side ListBox and Click on '' button then your selected item such as JavaScript can move to right side ListBox.

ASP. NET 4.5.2 Hosting : Move Items from One ListBox to Another ListBox working with C#/VB. NET

0 comments:

Post a Comment

thanks for your comment!