基本情况
效果图:
![](https://images.cnblogs.com/cnblogs_com/fangqing1986/listbox.JPG)
左移效果:
![](https://images.cnblogs.com/cnblogs_com/fangqing1986/listbox2.jpg)
其余效果:右边的listbox进行上下移动。
listbox的属性设置为:Multiple
解决方案(一):
调试结果:PASS (√)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
public partial class Default10 : System.Web.UI.Page
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
protected void Page_Load(object sender, EventArgs e)
5![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
//如果listbox1集合总数>0,则‘右移’按钮状态为真
7
if ((this.ListBox1.Items.Count > 0)&&(this.ListBox2.Items.Count>0))
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
this.btAdd.Enabled = true;
10
}
11
else
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
this.btAdd.Enabled = false;
14
}
15
//如果listbox2集合总数>0,则‘左移’按钮状态为真
16
if(this.ListBox2.Items.Count>0)
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
this.btAddAll.Enabled = true;
19
}
20
else
21![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
this.btAddAll.Enabled = false;
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
//右移
28
protected void btAdd_Click(object sender, EventArgs e)
29![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
try
31![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
string listBox1 = this.ListBox1.SelectedItem.Text;
33
int i = this.ListBox2.SelectedIndex;
34
int j = this.ListBox1.Items.Count;
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
if (j == 0)
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
this.btAdd.Enabled = false;
39
}
40
else
41![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
this.btAdd.Enabled = true;
43
}
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
this.ListBox2.Items.Add(listBox1);
46
this.ListBox1.Items.Remove(listBox1);
47
48
}
49
catch (Exception)
50![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
Response.Write("<script language =javascript>alert('请选择元素!')</script>");
52
}
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
//左移
56
protected void btAddAll_Click(object sender, EventArgs e)
57![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
try
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
if (this.ListBox2.Items.Count == 0)
61![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
this.btAddAll.Enabled = false;
63
}
64
else
65![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
Response.Write("<script language =javascript>alert('请选择元素!')</script>");
75
}
76
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
//清除listbox2所有内容
80
protected void btRemove_Click(object sender, EventArgs e)
81![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
this.ListBox2.Items.Clear();
83
}
84
//PostBack事件1
85
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
86![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
if (this.ListBox2.Items.Count == 0)
88![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
this.btAddAll.Enabled = false;
90
}
91
}
92
//PostBack事件2
93
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
94![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
if (this.ListBox1.Items.Count == 0)
96![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
this.btAdd.Enabled = false;
98
}
99
else
100![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
this.btAdd.Enabled=true;
102
}
103
}
104
105
//上移
106
protected void Button4_Click(object sender, EventArgs e)
107![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
try
109![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
Response.Write("<script language =javascript>alert('请选择元素!')</script>");
123
}
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
}
126
//下移
127
protected void Button5_Click1(object sender, EventArgs e)
128![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
try
130![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
143
Response.Write("<script language =javascript>alert('请选择元素!')</script>");
144
}
145
}
146
}
147
解决方案(二):
调试结果:PASS (√)
结果:可操作性灵活,数据不会异常
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
public partial class _Default : System.Web.UI.Page
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
protected void Page_Load(object sender, EventArgs e)
5![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
}
8
protected void Button1_Click(object sender, EventArgs e)
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
//定义中间动态存储
11
ArrayList arrRight=new ArrayList();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
//读取左边listbox的item的选中项
14
foreach (ListItem item in this.leftListBox.Items)
15![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
if (item.Selected)
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
arrRight.Add(item);
19
}
20
}
21
//执行右移操作
22
foreach (ListItem item in arrRight)
23![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
this.rightListBox.Items.Add(item);
25
this.leftListBox.Items.Remove(item);
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
}
29
protected void Button2_Click(object sender, EventArgs e)
30![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
ArrayList arrLeft = new ArrayList();
32
//读取右边listboxitem的选中项
33
foreach (ListItem item in this.rightListBox.Items)
34![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
if (item.Selected)
36![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
arrLeft.Add(item);
38
}
39
}
40
//执行左移操作
41
foreach (ListItem item in arrLeft)
42![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
this.leftListBox.Items.Add(item);
44
this.rightListBox.Items.Remove(item);
45
}
46
}
47
}
48
转载于:https://www.cnblogs.com/fangqing1986/archive/2008/08/23/1274756.html