2个Listbox 之间数据的左右移动,及上下移动
基本情况
效果图:
左移效果:

其余效果:右边的listbox进行上下移动。
listbox的属性设置为:Multiple
解决方案(一):
调试结果:PASS (√)
1

2
public partial class Default10 : System.Web.UI.Page3


{4
protected void Page_Load(object sender, EventArgs e)5

{6
//如果listbox1集合总数>0,则‘右移’按钮状态为真7
if ((this.ListBox1.Items.Count > 0)&&(this.ListBox2.Items.Count>0))8

{9
this.btAdd.Enabled = true;10
}11
else12

{13
this.btAdd.Enabled = false;14
}15
//如果listbox2集合总数>0,则‘左移’按钮状态为真16
if(this.ListBox2.Items.Count>0)17

{18
this.btAddAll.Enabled = true;19
}20
else21

{22
this.btAddAll.Enabled = false;23
}24

25
}26

27
//右移28
protected void btAdd_Click(object sender, EventArgs e)29

{30
try31

{32
string listBox1 = this.ListBox1.SelectedItem.Text;33
int i = this.ListBox2.SelectedIndex;34
int j = this.ListBox1.Items.Count;35

36
if (j == 0)37

{38
this.btAdd.Enabled = false;39
}40
else41

{42
this.btAdd.Enabled = true;43
}44

45
this.ListBox2.Items.Add(listBox1);46
this.ListBox1.Items.Remove(listBox1);47
48
}49
catch (Exception)50

{51
Response.Write("<script language =javascript>alert('请选择元素!')</script>");52
}53
}54

55
//左移56
protected void btAddAll_Click(object sender, EventArgs e)57

{58
try59

{60
if (this.ListBox2.Items.Count == 0)61

{62
this.btAddAll.Enabled = false;63
}64
else65

{66
this.btAddAll.Enabled = true;67
}68
string listBox2 = this.ListBox2.SelectedItem.Text;69
this.ListBox2.Items.Remove(listBox2);70
this.ListBox1.Items.Add(listBox2);71
}72
catch(Exception)73

{74
Response.Write("<script language =javascript>alert('请选择元素!')</script>");75
}76
77
}78

79
//清除listbox2所有内容80
protected void btRemove_Click(object sender, EventArgs e)81

{82
this.ListBox2.Items.Clear();83
}84
//PostBack事件185
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)86

{87
if (this.ListBox2.Items.Count == 0)88

{89
this.btAddAll.Enabled = false;90
}91
}92
//PostBack事件293
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)94

{95
if (this.ListBox1.Items.Count == 0)96

{97
this.btAdd.Enabled = false;98
}99
else100

{101
this.btAdd.Enabled=true;102
}103
}104
105
//上移106
protected void Button4_Click(object sender, EventArgs e)107

{108
try109

{110
string item = this.ListBox2.SelectedItem.Text;111
int i = this.ListBox2.SelectedIndex;112
int j = this.ListBox2.Items.Count;113
if ((j > 0) && (i != 0))114

{115
this.ListBox2.Items.Remove(this.ListBox2.SelectedItem.Text);116
this.ListBox2.Items.Insert(i - 1, item);117
this.ListBox2.SelectedIndex = i - 1;118
}119
}120
catch (Exception)121

{122
Response.Write("<script language =javascript>alert('请选择元素!')</script>");123
}124

125
}126
//下移127
protected void Button5_Click1(object sender, EventArgs e)128

{129
try130

{131
string item = this.ListBox2.SelectedItem.Text;132
int i = this.ListBox2.SelectedIndex;133
int j = this.ListBox2.Items.Count;134
if ((i < j - 1) && (j > 0))135

{136
this.ListBox2.Items.Remove(this.ListBox2.SelectedItem.Text);137
this.ListBox2.Items.Insert(i + 1, item);138
this.ListBox2.SelectedIndex = i + 1;139
}140
}141
catch (Exception)142

{143
Response.Write("<script language =javascript>alert('请选择元素!')</script>");144
}145
}146
}147

解决方案(二):
调试结果:PASS (√)
结果:可操作性灵活,数据不会异常
1

2
public partial class _Default : System.Web.UI.Page3


{4
protected void Page_Load(object sender, EventArgs e)5

{6

7
}8
protected void Button1_Click(object sender, EventArgs e)9

{10
//定义中间动态存储11
ArrayList arrRight=new ArrayList();12

13
//读取左边listbox的item的选中项14
foreach (ListItem item in this.leftListBox.Items)15

{16
if (item.Selected)17

{18
arrRight.Add(item);19
}20
}21
//执行右移操作22
foreach (ListItem item in arrRight)23

{24
this.rightListBox.Items.Add(item);25
this.leftListBox.Items.Remove(item);26
}27

28
}29
protected void Button2_Click(object sender, EventArgs e)30

{31
ArrayList arrLeft = new ArrayList();32
//读取右边listboxitem的选中项33
foreach (ListItem item in this.rightListBox.Items)34

{35
if (item.Selected)36

{37
arrLeft.Add(item);38
}39
}40
//执行左移操作41
foreach (ListItem item in arrLeft)42

{43
this.leftListBox.Items.Add(item);44
this.rightListBox.Items.Remove(item);45
}46
}47
}48

浙公网安备 33010602011771号