Tuesday 29 October 2013

ASP.NET: Move Items from One ListBox to Another ListBox using C#/VB.NET

Hi friends,in this article i will explain about ASP.NET Move Items from One ListBox to Another ListBox.
I already explained in the previous articles about How to Bind Nested Gridview from Database in asp.net with C#/VB.NET,Marquee tag or How to Scroll Text From left to right or How to Move the Text in HTML,C#/VB.NET:Save the generated pdf directly to the server directory folder without user prompt in ASP.NET and JQuery:How to Display Current Date and Time on Webpage using ASP.NET.
In ASP.NET write the following code.
<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>

In 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 of the above program as shown in the below figure.when you select the item in left side ListBox and Click on '>' button then your selected item like JavaScript will move to right side ListBox and vice versa.

 "If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.