zoukankan
html css js c++ java
两个ListBox的互动
适合:
.Net 2.0
VS2005
效果:
ASPX页面:
<
table
>
<
tbody
>
<
tr
>
<
td
>
<
asp:ListBox ID
=
"
lbLeft
"
runat
=
"
server
"
SelectionMode
=
"
Multiple
"
>
<
asp:ListItem
>
添加名字
</
asp:ListItem
>
<
asp:ListItem
>
出生年月
</
asp:ListItem
>
</
asp:ListBox
>
</
td
>
<
td style
=
"
27px
"
>
<
asp:Button ID
=
"
btnToRight
"
runat
=
"
server
"
Text
=
"
>>
"
OnClick
=
"
btnToRight_Click
"
/>
<
br
/>
<
asp:Button ID
=
"
btnToLeft
"
runat
=
"
server
"
Text
=
"
<<
"
OnClick
=
"
btnToLeft_Click
"
/>
</
td
>
<
td style
=
"
3px
"
>
<
asp:ListBox ID
=
"
lbRight
"
runat
=
"
server
"
SelectionMode
=
"
Multiple
"
></
asp:ListBox
></
td
>
</
tr
>
</
tbody
>
</
table
>
<
asp:Label ID
=
"
lblMsg
"
runat
=
"
server
"
></
asp:Label
>
CS Code :
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
Test1 : System.Web.UI.Page
{
protected
void
Page_Load (
object
sender , EventArgs e )
{
}
protected
void
btnToRight_Click(
object
sender, EventArgs e)
{
if
(lbLeft.SelectedItem
!=
null
)
{
AddItemFromSourceListBox(lbLeft, lbRight);
RemoveSelectedItem(lbLeft);
lblMsg.Text
=
""
;
//
注意:为什么要这一行?
foreach
(ListItem item
in
lbRight.Items)
{
if
(item.Selected)
lblMsg.Text
+=
item.Text;
}
}
}
protected
void
btnToLeft_Click(
object
sender, EventArgs e)
{
if
(lbRight.SelectedItem
!=
null
)
{
AddItemFromSourceListBox(lbRight, lbLeft);
RemoveSelectedItem(lbRight);
}
}
private
void
RemoveSelectedItem(ListBox listControl)
{
while
(listControl.SelectedIndex
!=
-
1
)
{
listControl.Items.RemoveAt(listControl.SelectedIndex);
}
}
private
void
AddItemFromSourceListBox(ListBox sourceBox,ListBox targetBox)
{
foreach
(ListItem item
in
sourceBox.Items)
{
if
(item.Selected
==
true
&&
!
targetBox.Items.Contains(item))
{
targetBox.Items.Add(item);
}
}
}
}
查看全文
相关阅读:
rabbitmq入门
php7.2 安装redis扩展
php安装扩展的几种方法
yum安装php7.2
相关报错
[枚举]P1089 津津的储蓄计划
[DFS]排列的生成
[枚举]P1085 不高兴的津津
[模拟]P1047 校门外的树
[模拟]P1046 陶陶摘苹果
原文地址:https://www.cnblogs.com/sinkzephyr/p/862632.html
最新文章
进程相关
线程相关
并发编程
计算机网络
网络编程最终章
java 移位运算符
异常
return和finally的执行和联系
Java中单例
多态.借口
热门文章
继承
面向对象开发
学生管理系统
使用using释放资源
学习事务,视图和索引
mysql 主从基本操作
mysql docker相关问题
五大常见的MySQL高可用方案
nginx常见bug
mongodb 连接工具
Copyright © 2011-2022 走看看