protected void btn_up_Click(object sender, EventArgs e)
...{
if (lb_busspot.SelectedIndex == -1)
...{
return;
}
//获得连续选中的项索引
int[] Indices = lb_busspot.GetSelectedIndices();
int length = Indices.Length;
string text;
string value;
//如果选择的最小索引是0,表示是最上面的项
if (Indices[0] == 0)
...{
return;
}
//判断选择多项时是否是连续的项
if (Indices.Length != 1 && Indices[0] + length - 1 != Indices[length - 1])
...{
MessageBox.Show(Page, "请选择连续的项!");
return;
}
//将选中的上面一项未选中的值赋予临时变量
text = lb_busspot.Items[Indices[0] - 1].Text;
value = lb_busspot.Items[Indices[0] - 1].Value;
for (int i = 0; i < length; i++)
...{
lb_busspot.Items[Indices[i] - 1].Text = lb_busspot.Items[Indices[i]].Text;
lb_busspot.Items[Indices[i] - 1].Value = lb_busspot.Items[Indices[i]].Value;
//保证被选中状态
lb_busspot.Items[Indices[i] - 1].Selected = true;
lb_busspot.Items[Indices[i]].Selected = false;
}
//将选中的上面第一条未选中的值赋予到下面
lb_busspot.Items[Indices[0] + length - 1].Text = text;
lb_busspot.Items[Indices[0] + length - 1].Value = value;
}
protected void btn_down_Click(object sender, EventArgs e)
...{
if (lb_busspot.SelectedIndex == -1)
...{
return;
}
//获得连续选中的项索引
int[] Indices = lb_busspot.GetSelectedIndices();
int length = Indices.Length;
string text;
string value;
//如果选择的是最底下的项
if (Indices[length - 1] == lb_busspot.Items.Count - 1)
...{
return;
}
//判断选择多项时是否是连续的项
if (Indices.Length != 1 && Indices[0] + length - 1 != Indices[length - 1])
...{
MessageBox.Show(Page, "请选择连续的项!");
return;
}
//将选中的下面一项未选中的值赋予临时变量
text = lb_busspot.Items[Indices[length - 1] + 1].Text;
value = lb_busspot.Items[Indices[length - 1] + 1].Value;
for (int i = length; i > 0; i--)
...{
lb_busspot.Items[Indices[i - 1] + 1].Text = lb_busspot.Items[Indices[i - 1]].Text;
lb_busspot.Items[Indices[i - 1] + 1].Value = lb_busspot.Items[Indices[i - 1]].Value;
//保证被选中状态
lb_busspot.Items[Indices[i - 1] + 1].Selected = true;
lb_busspot.Items[Indices[i - 1]].Selected = false;
}
//将下面第一条未选中的项的值赋予到上面
lb_busspot.Items[Indices[0]].Text = text;
lb_busspot.Items[Indices[0]].Value = value;
}
本文介绍了一种在Web应用程序中实现列表项上下移动的方法,通过按钮点击事件处理,可以调整列表中选定项的位置,同时确保操作的连贯性和正确性。
3393

被折叠的 条评论
为什么被折叠?



