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

关于Button控件的CommandName属性用法的一个实例的更多相关文章

  1. ASP.NET Button控件的UseSubmitBehavior属性引发的血案

    这里先不说标题上的UseSubmitBehavior属性是什么,先说下面这种情况. 通常,在我们写一个表单页面的时候,最下方会有“提交”和“返回”字样的两个按钮.顾名思义,它们的功能大家都知道,但是一 ...

  2. ASP.NET中Button控件的CommandName和CommandArgument属性用法

    在Repeater中的使用: <asp:Repeater ID="rptActionList" runat="server" OnItemCommand= ...

  3. C# Windows - Button 控件

    .Net Framework提供了一个派生于Control的类System.Windows.Forms.ButtonBase,它实现了Button控件所需的基本功能. System.Windows.F ...

  4. asp.net button控件 使用JS的 disabled

     今天想用JS禁用asp.net的button控件,查了好久,都是一行代码....      document.getElementById("Button1").disabled ...

  5. WPF中Image控件的Source属性

    原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...

  6. C#控件方法及属性大全,望补充

    C#控件及常用设计整理 1.窗体 常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体. (2) WindowState属性: 用来获取或设置窗体的窗口状态 ...

  7. Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全

    原文:Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全 前面简单学习了一些Android UI的一些基础知识,那么接下来我们一起来详细学习Android的 ...

  8. C#控件及常用属性

    1.窗体(Form) 1.常用属性 (1)Name 属性:用来获取或设置窗体的名称,在应用程序中可通过Name 属性来引用窗体. (2) WindowState 属性: 用来获取或设置窗体的窗口状态. ...

  9. WPF--Blend制作Button控件模板--问题补充

    补充记录Button控件模板 控件模板制作过程中出现下图问题:动画对象不能用于动画属性"Fill” 并且这类问题Blend4中包括VS2010中仍然可以运行,但是只有VS2010中会报错:如 ...

随机推荐

  1. NX二次开发-UFUN特征找xxx UF_MODL_ask_feat_xxx等函数(待补充)

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> #include <u ...

  2. csp-s模拟测试91

    csp-s模拟测试91 倒悬吃屎的一套题. $T1$认真(?)分析题意发现复杂度不能带$n$(?),计划直接维护答案,考虑操作对答案的影响,未果.突然发现可以动态开点权值线段树打部分分,后来$Tm$一 ...

  3. 常用Git命令以及出现的状况ing

    (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 我的GitHub: Cwolf9 下面是我学习Git时了解到的一些命令和状况经验. 把它们记下来免得忘了.就算忘了也有地方看... 状 ...

  4. go语言type使用小技巧

    import "fmt" type Rank int const ( Rank001 Rank = iota Rank002 Rank003 Rank004 ) var rewar ...

  5. fastText一个库用于词表示的高效学习和句子分类

    fastText fastText 是 Facebook 开发的一个用于高效学习单词呈现以及语句分类的开源库. 要求 fastText 使用 C++11 特性,因此需要一个对 C++11 支持良好的编 ...

  6. .Net Core 部署之一 《CentOS 从GitHub/Gitee 等源代码网站部署Web网站》

    先看下楼主从某阿打折购买的渣渣服务器 lsb_release -a 一.安装dotnet-SDK 注册微软的服务 sudo rpm -Uvh https://packages.microsoft.co ...

  7. Codeforces 1163A - Eating Soup

    题目链接:http://codeforces.com/problemset/problem/1163/A 题意:n 只猫围成一圈,离开 m 只,最多剩下几组猫. 思路:当 n == m 即猫都离开时 ...

  8. 什么是css块级元素和内联元素

    CSS文档流与块级元素(block).内联元素(inline),文档流这个概念理解了它,一堆CSS布局的理论都 变得易于理解,并且体会到CSS这套设计的合理性所在. 文档流 将窗体自上而下分成一行行, ...

  9. SpringCloud学习笔记《---06 Config 分布式配置中心---》基础篇

  10. Windows中的"簇"和Linux中的"块"是对应的

    扇区是对硬盘而言,块是对文件系统而言. 簇”又称为“分配单元” ,文件系统是操作系统与驱动器之间的接口,当操作系统请求从硬盘里读取一个文件时,会请求相应的文件系统(FAT 16/32/NTFS)打开文 ...