Create new Website and in Default.aspx add two Listbox (ListBox1 , ListBox2) and add items in Listbox1 and add two buttons (<< ,>>) with Id's
(btnMoveLeft,btnMoveRight)
The Javascript code will be:
//function fnMoveItems(lstbxFrom,lstbxTo)
//{
// var varFromBox = document.all(lstbxFrom);
// var varToBox = document.all(lstbxTo);
// if ((varFromBox != null) && (varToBox != null))
// {
// if(varFromBox.length < 1)
// {
// alert('There are no items in the source ListBox');
// return false;
// }
// if(varFromBox.options.selectedIndex == -1)
// when no Item is selected the index will be -1
// {
// alert('Please select an Item to move');
// return false;
// }
// while ( varFromBox.options.selectedIndex >= 0 )
// {
// Create a new instance of ListItem
// var newOption = new Option();
// newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
// newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
//Append the item in Target Listbox
// varToBox.options[varToBox.length] = newOption;
//Remove the item from Source Listbox
// varFromBox.remove(varFromBox.options.selectedIndex);
// }
// }
// return false;
//}
and in Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnMoveRight.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')");
btnMoveLeft.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')");
}
}
Monday, March 31, 2008
Move item from Listbox to another (2)
Create new Website and in Default.aspx add two Listbox (lstEmployees , lstSelectedEmployees) and in lstEmployees Listbox add items and add four buttons (<< ,>>,<,>) with Id's
(btn_AddAll,btn_RemoveAll,btn_Add,btn_Remove)
and in Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstEmployees.SelectedItem.Value;
// Gets the Text of items in the list.
string _text = lstEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
//Assign the values to list item
item.Text = _text;
item.Value = _value;
//Add the list item to the selected list of employees
lstSelectedEmployees.Items.Add(item);
//Remove the details from employee list
lstEmployees.Items.Remove(item);
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstSelectedEmployees.SelectedItem.Value;
//Gets the Text of items in the list.
string _text = lstSelectedEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
item.Text = _text;
//Assign the values to list item
item.Value = _value;
//Remove from the selected list
lstSelectedEmployees.Items.Remove(item);
//Add in the Employee list
lstEmployees.Items.Add(item);
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
//clear the items
lstSelectedEmployees.Items.Clear();
}
}
(btn_AddAll,btn_RemoveAll,btn_Add,btn_Remove)
and in Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstEmployees.SelectedItem.Value;
// Gets the Text of items in the list.
string _text = lstEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
//Assign the values to list item
item.Text = _text;
item.Value = _value;
//Add the list item to the selected list of employees
lstSelectedEmployees.Items.Add(item);
//Remove the details from employee list
lstEmployees.Items.Remove(item);
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstSelectedEmployees.SelectedItem.Value;
//Gets the Text of items in the list.
string _text = lstSelectedEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
item.Text = _text;
//Assign the values to list item
item.Value = _value;
//Remove from the selected list
lstSelectedEmployees.Items.Remove(item);
//Add in the Employee list
lstEmployees.Items.Add(item);
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
//clear the items
lstSelectedEmployees.Items.Clear();
}
}
Moving Item from listbox to another (1)
Create new Website and in Default.aspx add two Listbox (lstEmployees , lstSelectedEmployees) and add six buttons (<< ,>>,<,>,^,v) with Id's
(btnremoveall,btnaddall,btnremoveitem,btnadditem,btnColmoveup,btnColmovedown)
and in Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
#region Methods
private void MoveUp(ListBox lstBox)
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData, lItemval;
try
{
// Get the count of items in the list control
iCount = lstBox.Items.Count;
// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = 0;
iOffset = -1;
// Loop through all of the items in the list.
while (iIndex < iCount)
{
// Check if this item is selected.
if (lstBox.SelectedIndex > 0)
{
// Get the item data for this item
lItemval = lstBox.SelectedItem.Value.ToString();
lItemData = lstBox.SelectedItem.Text.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;
// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position
lstBox.Items.RemoveAt(iIndex2);
// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;
}
}
}
}
// If this item wasn't selected save the index so we can check it later so we don't move past the any selected items.
else if (-1 == iIndexSelectedMarker)
{
iIndexSelectedMarker = iIndex;
break;
}
iIndex = iIndex + 1;
}
if (iIndexSelectedMarker == 0)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker - 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void MoveDown(ListBox lstBox)
{
try
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData;
string lItemval;
// Get the count of items in the list control
iCount = lstBox.Items.Count;
// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = iCount - 1;
iOffset = 1;
// Loop through all of the items in the list.
while (iIndex >= 0)
{
// Check if this item is selected.
if (lstBox.SelectedIndex >= 0)
{
// Get the item data for this item
lItemData = lstBox.SelectedItem.Text.ToString();
lItemval = lstBox.SelectedItem.Value.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;
// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount - 1; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position
lstBox.Items.RemoveAt(iIndex2);
// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;
}
}
}
}
iIndex = iIndex - 1;
}
if (iIndexSelectedMarker == lstBox.Items.Count - 1)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker + 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void AddRemoveAll(ListBox aSource, ListBox aTarget)
{
try
{
foreach (ListItem item in aSource.Items)
{
aTarget.Items.Add(item);
}
aSource.Items.Clear();
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void AddRemoveItem(ListBox aSource, ListBox aTarget)
{
ListItemCollection licCollection;
try
{
licCollection = new ListItemCollection();
for (int intCount = 0; intCount < aSource.Items.Count; intCount++)
{
if (aSource.Items[intCount].Selected == true)
licCollection.Add(aSource.Items[intCount]);
}
for (int intCount = 0; intCount < licCollection.Count; intCount++)
{
aSource.Items.Remove(licCollection[intCount]);
aTarget.Items.Add(licCollection[intCount]);
}
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
finally
{
licCollection = null;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnaddall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstEmployees, lstSelectedEmployees);
}
protected void btnadditem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstEmployees, lstSelectedEmployees);
}
protected void btnremoveitem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstSelectedEmployees, lstEmployees);
}
protected void btnremoveall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstSelectedEmployees, lstEmployees);
}
protected void btnColmoveup_Click(object sender, EventArgs e)
{
MoveUp(lstSelectedEmployees);
}
protected void btnColmovedwn_Click(object sender, EventArgs e)
{
MoveDown(lstSelectedEmployees);
}
}
(btnremoveall,btnaddall,btnremoveitem,btnadditem,btnColmoveup,btnColmovedown)
and in Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
#region Methods
private void MoveUp(ListBox lstBox)
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData, lItemval;
try
{
// Get the count of items in the list control
iCount = lstBox.Items.Count;
// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = 0;
iOffset = -1;
// Loop through all of the items in the list.
while (iIndex < iCount)
{
// Check if this item is selected.
if (lstBox.SelectedIndex > 0)
{
// Get the item data for this item
lItemval = lstBox.SelectedItem.Value.ToString();
lItemData = lstBox.SelectedItem.Text.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;
// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position
lstBox.Items.RemoveAt(iIndex2);
// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;
}
}
}
}
// If this item wasn't selected save the index so we can check it later so we don't move past the any selected items.
else if (-1 == iIndexSelectedMarker)
{
iIndexSelectedMarker = iIndex;
break;
}
iIndex = iIndex + 1;
}
if (iIndexSelectedMarker == 0)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker - 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void MoveDown(ListBox lstBox)
{
try
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData;
string lItemval;
// Get the count of items in the list control
iCount = lstBox.Items.Count;
// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = iCount - 1;
iOffset = 1;
// Loop through all of the items in the list.
while (iIndex >= 0)
{
// Check if this item is selected.
if (lstBox.SelectedIndex >= 0)
{
// Get the item data for this item
lItemData = lstBox.SelectedItem.Text.ToString();
lItemval = lstBox.SelectedItem.Value.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;
// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount - 1; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position
lstBox.Items.RemoveAt(iIndex2);
// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;
}
}
}
}
iIndex = iIndex - 1;
}
if (iIndexSelectedMarker == lstBox.Items.Count - 1)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker + 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void AddRemoveAll(ListBox aSource, ListBox aTarget)
{
try
{
foreach (ListItem item in aSource.Items)
{
aTarget.Items.Add(item);
}
aSource.Items.Clear();
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}
private void AddRemoveItem(ListBox aSource, ListBox aTarget)
{
ListItemCollection licCollection;
try
{
licCollection = new ListItemCollection();
for (int intCount = 0; intCount < aSource.Items.Count; intCount++)
{
if (aSource.Items[intCount].Selected == true)
licCollection.Add(aSource.Items[intCount]);
}
for (int intCount = 0; intCount < licCollection.Count; intCount++)
{
aSource.Items.Remove(licCollection[intCount]);
aTarget.Items.Add(licCollection[intCount]);
}
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
finally
{
licCollection = null;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnaddall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstEmployees, lstSelectedEmployees);
}
protected void btnadditem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstEmployees, lstSelectedEmployees);
}
protected void btnremoveitem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstSelectedEmployees, lstEmployees);
}
protected void btnremoveall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstSelectedEmployees, lstEmployees);
}
protected void btnColmoveup_Click(object sender, EventArgs e)
{
MoveUp(lstSelectedEmployees);
}
protected void btnColmovedwn_Click(object sender, EventArgs e)
{
MoveDown(lstSelectedEmployees);
}
}
Subscribe to:
Posts (Atom)

