您的位置:首页 > 编程语言 > ASP

ListBox控件实现上移、下移、循环上移、循环下移操作

2016-04-10 12:40 701 查看
一、先在前台页面中拖入一个listbox控件和四个button控件,可以对它们的样式进行一下修改。

<style type="text/css">
.left {
float:left;
width:120px;

}
.right {
float:right;
width:80px;
}
.all {
width:200px;
}
</style>

二,在后台相对应的button按钮的click方法中加入代码。
protected void Button1_Click(object sender, EventArgs e)
{
//ListBox1.Items.Remove(ListBox1.SelectedItem);
//上移
if (ListBox1.SelectedIndex > 0) {
int idx = ListBox1.SelectedIndex;
ListBox1.Items.Insert(ListBox1.SelectedIndex - 1, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
ListBox1.SelectedIndex = idx - 1;
}
}

protected void Button2_Click(object sender, EventArgs e)
{
//下移
if (ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
{
ListBox1.Items.Insert(ListBox1.SelectedIndex, ListBox1.Items[ListBox1.SelectedIndex + 1].ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex + 1);
}
}

protected void Button3_Click(object sender, EventArgs e)
{
//循环上移
if (ListBox1.SelectedIndex == 0) {
ListBox1.Items.Insert(ListBox1.Items.Count,ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}

protected void Button4_Click(object sender, EventArgs e)
{
//循环下移
if (ListBox1.SelectedIndex == ListBox1.Items.Count-1) {
ListBox1.Items.Insert(0, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  listbox 控件 asp.net