
I decided to extend the existing list box and create a new listbox called SmartListBox. This implements two methods MoveUp and MoveDown that moves the selected items up or down corresponding on the method invoked, in addition to all the methods inherited from ListBox. The code is:
public class SmartListBox : ListBox
{
//Moves the selected items up one level
public MoveUp()
{
for(int i = 0; i < Items.Count; i++)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the top item(move up)
if (i > 0 && !Items[i - 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i - 1, bottom);
Items[i - 1].Selected = true;
}
}
}
}
//Moves the selected items one level down
public MoveDown()
{
int startindex = Items.Count -1;
for (int i = startindex; i > -1; i--)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the lower item(move down)
if (i < startindex && !Items[i + 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i + 1, bottom);
Items[i + 1].Selected = true;
}
public class SmartListBox : ListBox
{
//Moves the selected items up one level
public MoveUp()
{
for(int i = 0; i < Items.Count; i++)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the top item(move up)
if (i > 0 && !Items[i - 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i - 1, bottom);
Items[i - 1].Selected = true;
}
}
}
}
//Moves the selected items one level down
public MoveDown()
{
int startindex = Items.Count -1;
for (int i = startindex; i > -1; i--)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the lower item(move down)
if (i < startindex && !Items[i + 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i + 1, bottom);
Items[i + 1].Selected = true;
}
}
}
}
}
}
}
This is the server side implementation and the event handler of the Up\Down button will invoke the MoveUp or MoveDown method on the listbox. The same logic can be written in javascript to provide client side implementation.

kalai romiPosted Nov 28, 2019, 3:46 AM
Can you post the full source code
C# CuratorPosted Nov 12, 2010, 7:05 AM
ggggggggggggggggg
Jaffreena MichaelthomasPosted Nov 12, 2010, 6:16 AM
This Works Perfectly!!!!!!
Cristian DelgadoPosted Feb 23, 2010, 6:12 PM
This implementation does it all on a single for loop, it's controlled by the index parameter, it must be -1 to move the item up (the index decreases) or 1 to move the item down (the index increases). It also works on a multiple selection listbox. The loop dynamically adjusts its direction depending on the index parameter: private void MoveListboxItem(int index, ListBox listBox) { if (listBox.SelectedIndex != -1) //is there an item selected? { //if it's moving up, the loop moves from first to last, otherwise, it moves from last to first for (int i = (index < 0 ? 0 : listBox.Items.Count - 1); index < 0 ? i < listBox.Items.Count : i > -1; i -= index) { if (listBox.Items[i].Selected) { //if it's moving up, it should not be the first item, or, if it's moving down, it should not be the last if ((index < 0 && i > 0) || (index > 0 && i < listBox.Items.Count - 1)) { //if it's moving up, the previous item should not be selected, or, if it's moving down, the following item should not be selected if ((index < 0 && !listBox.Items[i - 1].Selected) || (index > 0 && !listBox.Items[i + 1].Selected)) { ListItem itemA = listBox.Items[i]; //the selected item listBox.Items.Remove(itemA); //is removed listBox.Items.Insert(i + index, itemA); //and swapped } } } } } }
jan blomquistPosted Feb 3, 2009, 11:52 AM
Same concept, different code, fully ajaxified and some additional event handlers ... http://blogs.gaiaware.net/post/Building-an-Ordered-Ajax-ListControl-in-Gaia-Ajax-using-C.aspx