注:本文分享于悠闲的博客,地址:http://www.cnblogs.com/9999/archive/2009/11/24/1609234.html
1、前台的代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyListBox.aspx.cs" Inherits="MyListBox" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 <style type="text/css"> 9 .style2 10 { 11 96px; 12 } 13 .style3 14 { 15 100px; 16 } 17 .style4 18 { 19 199px; 20 } 21 </style> 22 </head> 23 <body bgcolor="#cccccc"> 24 <form id="form1" runat="server"> 25 <div> 26 27 <table> 28 <tr> 29 <td class="style2" rowspan="6"> 30 <asp:ListBox ID="SourceList" runat="server" Height="160px" Width="200px"> 31 </asp:ListBox> 32 </td> 33 <td class="style3"> 34 </td> 35 <td class="style4" rowspan="6"> 36 <asp:ListBox ID="DirectList" runat="server" Height="160px" Width="200px"></asp:ListBox> 37 </td> 38 </tr> 39 <tr> 40 <td class="style3" align="center"> 41 <asp:Button ID="btnAddOne" runat="server" Text=">" CommandName="addOne" 42 Height="25px" onclick="AddAndDelete_Command" Width="50px" /> 43 </td> 44 </tr> 45 <tr> 46 <td class="style3" align="center"> 47 <asp:Button ID="btnAddAll" runat="server" Text=">>" CommandName="addAll" 48 Height="25px" onclick="AddAndDelete_Command" Width="50px" /> 49 </td> 50 </tr> 51 <tr> 52 <td class="style3" align="center"> 53 <asp:Button ID="btnDeleteOne" runat="server" Text="<" 54 CommandName="deleteOne" Height="25px" onclick="AddAndDelete_Command" 55 Width="50px" /> 56 </td> 57 </tr> 58 <tr> 59 <td class="style3" align="center"> 60 <asp:Button ID="btnDeleteAll" runat="server" Text="<<" 61 CommandName="deleteAll" Height="25px" onclick="AddAndDelete_Command" 62 Width="50px" /> 63 </td> 64 </tr> 65 <tr> 66 <td class="style3"> 67 </td> 68 </tr> 69 </table> 70 71 <br /> 72 <asp:Label ID="lblSucessMessage" runat="server" Text="请选中列表控件中的数据"></asp:Label> 73 74 </div> 75 </form> 76 </body> 77 </html>
2、后台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 using System.Data.SqlClient; 9 using System.Configuration; 10 11 public partial class MyListBox : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 if (!IsPostBack) 16 { 17 //让按钮事件和AddAndDelete_Command方法建立关联 18 this.btnAddOne.Command += new CommandEventHandler(this.AddAndDelete_Command); 19 this.btnAddAll.Command += new CommandEventHandler(this.AddAndDelete_Command); 20 this.btnDeleteOne.Command += new CommandEventHandler(this.AddAndDelete_Command); 21 this.btnDeleteAll.Command += new CommandEventHandler(this.AddAndDelete_Command); 22 23 //另一种建立关联的写法 24 //this.btnAddOne.Click += new EventHandler(this.AddAndDelete_Command); 25 //this.btnAddAll.Click += new EventHandler(this.AddAndDelete_Command); 26 //this.btnDeleteOne.Click += new EventHandler(this.AddAndDelete_Command); 27 //this.btnDeleteAll.Click += new EventHandler(this.AddAndDelete_Command); 28 29 //加载并显示数据 30 GetUserName(); 31 } 32 } 33 34 //加载数据,绑定到SourceList控件 35 private void GetUserName() 36 { 37 //清空ListBox控件的所有数据项 38 SourceList.Items.Clear(); 39 40 //连接、读取数据,并把数据绑定到SourceList控件 41 string con = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString; 42 SqlConnection myCon = new SqlConnection(con); 43 string cmdText = "SELECT UI_UserID,UI_UserName FROM tb_UserInfo ORDER BY UI_UserID"; 44 SqlCommand myCom = new SqlCommand(cmdText, myCon); 45 46 myCon.Open(); 47 SqlDataReader myReader = myCom.ExecuteReader(); 48 49 while (myReader.Read()) 50 { 51 SourceList.Items.Add(new ListItem(myReader["UI_UserName"].ToString(), myReader["UI_UserID"].ToString())); 52 } 53 54 myReader.Close(); 55 myCon.Close(); 56 } 57 58 //根据按钮控件的CommandName属性进行判断而进行不同的操作 59 public void AddAndDelete_Command(object sender, System.EventArgs e) 60 { 61 string commandName = ((Button)sender).CommandName; 62 63 switch (commandName) 64 { 65 case "addOne": 66 if (SourceList.SelectedIndex > -1) 67 { 68 DirectList.Items.Add(SourceList.SelectedItem); 69 lblSucessMessage.Visible = false; 70 } 71 else 72 { 73 lblSucessMessage.Visible = true; 74 } 75 break; 76 case "addAll": 77 lblSucessMessage.Visible = false; 78 DirectList.Items.Clear(); 79 foreach (ListItem item in SourceList.Items) 80 { 81 DirectList.Items.Add(item); 82 } 83 break; 84 case "deleteOne": 85 if (DirectList.SelectedIndex > -1) 86 { 87 DirectList.Items.Remove(DirectList.SelectedItem); 88 lblSucessMessage.Visible = false; 89 } 90 else 91 { 92 lblSucessMessage.Visible = true; 93 } 94 break; 95 case "deleteAll": 96 lblSucessMessage.Visible = false; 97 DirectList.Items.Clear(); 98 break; 99 default: break; 100 } 101 102 //清空两个列表控件的选项 103 SourceList.SelectedIndex = -1; 104 DirectList.SelectedIndex = -1; 105 } 106 }