关于Button控件的CommandName属性用法的一个实例

时间:2023-03-10 01:50:53
关于Button控件的CommandName属性用法的一个实例

注:本文分享于悠闲的博客,地址: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 = -;
}
}