Saturday 3 August 2013

Move Items From one ListBox to another ListBox in Asp.Net




Description:-

                        In this Example we Explain that How to Move Items of one ListBox to another ListBox.in which user can select multiple items from the ListBox because we set SelectionMode="Multiple"

What is ListBox:-

           The ListBox control is used to create a single- or multi-selection drop-down list.

Some Important Property of the ListBox are as Follows:

·         Rows : The number of rows displayed in the list.
·         SelectionMode : Allows single or multiple selections.
·         AutoPostBack : Gets or sets a value indicating whether a postback to the server automatically occurs when the user changes the list selection.
·         DataSource : Gets or sets the object from which the data-bound control retrieves its list of data items.
·         DataSourceID : Gets or sets the ID of the control from which the data-bound control retrieves its list of data items.
·         DataTextField : Gets or sets the field of the data source that provides the text content of the list items.
·         DataValueField : Gets or sets the field of the data source that provides the value of each list item.
·         Items : Gets the collection of items in the list control.
·         Rows : Gets or sets the number of rows displayed in the ListBox Control.
·         SelectedIndex : Gets or sets the lowest ordinal index of the selected items in the list.
·         SelectedItem : Gets the selected item with the lowest index in the list control.
·         SelectedValue : Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value.

to show Example of how to send SMS in asp.net click here Send SMS in Asp.Net

to show Example of How to Read and write file in Asp.Net Read and Write Text File in asp.Net

to show Example of How to Rotate the Ads or Advertised without Refreshing the page Rotate Ads in Asp.Net



Default.aspx:-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:listbox ID="ListBox1" runat="server" SelectionMode="Multiple">
           <asp:listitem Value="0">Asp.Net</asp:listitem>
<asp:listitem Value="1">Apple</asp:listitem>
<asp:listitem Value="2">Banana</asp:listitem>
<asp:listitem Value="3">water lemon</asp:listitem>

      </asp:listbox>
        <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple">
             <asp:listitem Value="0">Asp.Net</asp:listitem>
<asp:listitem Value="1">Roti</asp:listitem>
<asp:listitem Value="2">Dal</asp:listitem>
<asp:listitem Value="3">sabji</asp:listitem>


        </asp:ListBox>

        <asp:Button ID="btn1" runat="server" Text=">" Width="45px" onclick="btn1_Click" />

<asp:Button ID="btn2" runat="server" Text=">>" Width="45px" onclick="btn2_Click" />

<asp:Button ID="btn3" runat="server" Text="<" Width="45px" onclick="btn3_Click" />

<asp:Button ID="btn4" runat="server" Text="<<" Width="45px" onclick="btn4_Click" />

        <asp:Label ID="lbltxt" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>




Default.aspx.cs:-




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
     ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn1_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox1.Items[i]))
{
arraylist1.Add(ListBox1.Items[i]);

}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox1.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox1 to move";
}
}

protected void btn2_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while(ListBox1.Items.Count!=0)
{
for(int i=0;i<ListBox1.Items.Count;i++)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}

protected void btn3_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (!arraylist2.Contains(ListBox2.Items[i]))
{
arraylist2.Add(ListBox2.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
{
ListBox1.Items.Add(((ListItem)arraylist2[i]));
}
ListBox2.Items.Remove(((ListItem)arraylist2[i]));
}
ListBox1.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox2 to move";
}
}

protected void btn4_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while (ListBox2.Items.Count != 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}

  

2 comments: