注:本文分享于悠闲的博客,地址:http://www.cnblogs.com/9999/archive/2009/11/24/1609234.html

1、前台的代码

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyListBox.aspx.cs" Inherits="MyListBox" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style2
{
width: 96px;
}
.style3
{
width: 100px;
}
.style4
{
width: 199px;
}
</style>
</head>
<body bgcolor="#cccccc">
<form id="form1" runat="server">
<div> <table>
<tr>
<td class="style2" rowspan="">
<asp:ListBox ID="SourceList" runat="server" Height="160px" Width="200px">
</asp:ListBox>
</td>
<td class="style3">
&nbsp;</td>
<td class="style4" rowspan="">
<asp:ListBox ID="DirectList" runat="server" Height="160px" Width="200px"></asp:ListBox>
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnAddOne" runat="server" Text="&gt;" CommandName="addOne"
Height="25px" onclick="AddAndDelete_Command" Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnAddAll" runat="server" Text="&gt;&gt;" CommandName="addAll"
Height="25px" onclick="AddAndDelete_Command" Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnDeleteOne" runat="server" Text="&lt;"
CommandName="deleteOne" Height="25px" onclick="AddAndDelete_Command"
Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnDeleteAll" runat="server" Text="&lt;&lt;"
CommandName="deleteAll" Height="25px" onclick="AddAndDelete_Command"
Width="50px" />
</td>
</tr>
<tr>
<td class="style3">
&nbsp;</td>
</tr>
</table> <br />
<asp:Label ID="lblSucessMessage" runat="server" Text="请选中列表控件中的数据"></asp:Label> </div>
</form>
</body>
</html>

2、后台代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; public partial class MyListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//让按钮事件和AddAndDelete_Command方法建立关联
this.btnAddOne.Command += new CommandEventHandler(this.AddAndDelete_Command);
this.btnAddAll.Command += new CommandEventHandler(this.AddAndDelete_Command);
this.btnDeleteOne.Command += new CommandEventHandler(this.AddAndDelete_Command);
this.btnDeleteAll.Command += new CommandEventHandler(this.AddAndDelete_Command); //另一种建立关联的写法
//this.btnAddOne.Click += new EventHandler(this.AddAndDelete_Command);
//this.btnAddAll.Click += new EventHandler(this.AddAndDelete_Command);
//this.btnDeleteOne.Click += new EventHandler(this.AddAndDelete_Command);
//this.btnDeleteAll.Click += new EventHandler(this.AddAndDelete_Command); //加载并显示数据
GetUserName();
}
} //加载数据,绑定到SourceList控件
private void GetUserName()
{
//清空ListBox控件的所有数据项
SourceList.Items.Clear(); //连接、读取数据,并把数据绑定到SourceList控件
string con = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString;
SqlConnection myCon = new SqlConnection(con);
string cmdText = "SELECT UI_UserID,UI_UserName FROM tb_UserInfo ORDER BY UI_UserID";
SqlCommand myCom = new SqlCommand(cmdText, myCon); myCon.Open();
SqlDataReader myReader = myCom.ExecuteReader(); while (myReader.Read())
{
SourceList.Items.Add(new ListItem(myReader["UI_UserName"].ToString(), myReader["UI_UserID"].ToString()));
} myReader.Close();
myCon.Close();
} //根据按钮控件的CommandName属性进行判断而进行不同的操作
public void AddAndDelete_Command(object sender, System.EventArgs e)
{
string commandName = ((Button)sender).CommandName; switch (commandName)
{
case "addOne":
if (SourceList.SelectedIndex > -)
{
DirectList.Items.Add(SourceList.SelectedItem);
lblSucessMessage.Visible = false;
}
else
{
lblSucessMessage.Visible = true;
}
break;
case "addAll":
lblSucessMessage.Visible = false;
DirectList.Items.Clear();
foreach (ListItem item in SourceList.Items)
{
DirectList.Items.Add(item);
}
break;
case "deleteOne":
if (DirectList.SelectedIndex > -)
{
DirectList.Items.Remove(DirectList.SelectedItem);
lblSucessMessage.Visible = false;
}
else
{
lblSucessMessage.Visible = true;
}
break;
case "deleteAll":
lblSucessMessage.Visible = false;
DirectList.Items.Clear();
break;
default: break;
} //清空两个列表控件的选项
SourceList.SelectedIndex = -;
DirectList.SelectedIndex = -;
}
}

最新文章

  1. 关于我-dinphy简介
  2. LabVIEW 吸星大法 - 看见的好东西都是我的(下篇)
  3. lintcode 滑动窗口的最大值(双端队列)
  4. Android 动态创建Fragment
  5. 将u盘的文件复制到虚拟机上的linux系统上面—》文件挂载(文字+图解)
  6. WinForm程序打包说明
  7. 【网络——Linux】——IPMI详细介绍【转】
  8. itextsharp去掉PDF加密
  9. JAVA HttpsURLConnection 忽略对SSL valid 的验证
  10. power shell upload file to azure storage
  11. POJ 3280 Cheapest Palindrome(水题)
  12. maven安装和配置及创建maven项目
  13. ServletConfig、ServletContext 的应用
  14. window 编译lua 5.3
  15. [No0000120]Python教程3/9-第一个Python程序
  16. gitblit.cmd运行自动关闭
  17. [svc]traceroute(udp+icmp)&amp;tracert(icmp)原理
  18. haproxy-1.7.7 源码安装
  19. JDK 12又来了,我学不动了...
  20. XML_Qt_资料

热门文章

  1. delphi mysql
  2. NX二次开发-UFUN设置环境变量UF_set_variable
  3. (转) MySQL中索引的限制
  4. 李宏毅机器学习课程---4、Gradient Descent (如何优化 )
  5. nc命令官方Demo实例
  6. class3_Entry &amp; Text 输入和文本框
  7. 1、linux常用命令的英文单词缩写
  8. eclispe 创建maven 项目:Could not resolve archetype org.apache.maven.archetypes
  9. 1.springboot+ActiveMQ
  10. dubbo视频分享