注:本文分享于悠闲的博客,地址: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删除工程图UF_DRAW_delete_drawing

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize ...

  2. POJ2516费用流

    目录 目录 (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门 AC代码: #include <iostream> #include <cstdio ...

  3. JVM内核-原理、诊断与优化学习笔记(十一):JVM字节码执行

    文章目录 javap javap 举个

  4. 【Web】浅析JQuery的apply(), call(), bind()方法

    原文地址:https://blog.csdn.net/whuzxq/article/details/64166253 由于在理解this的用法的时候多次出现了这几个方法,个人对这几个方法理解的不是很透 ...

  5. OSI七层网络模型分别是哪七层?各运行那些协议?

    本文摘自:https://blog.csdn.net/JeremyZJM/article/details/78184775 应用层 DHCP · DNS · FTP · Gopher · HTTP · ...

  6. 如何在html中添加视频

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="445" heig ...

  7. mysql的JDBC连接

    程序是通过DriverManager注册驱动,所以加载之后可以直接使用DriverMannagermysql中的多态: 不仅是赋值的时候使用了多态,返回的时候都是返回的借口(不是返回的子类对象),所以 ...

  8. Linux服务器下对Oracle数据库expdp(导出)和impdp(导入)

    紧接上篇文章,Oracle数据库架构已经创建完成,我的需求是:将老服务器上的数据库迁移到新的数据库上. 这就用到impdp(导入)操作. 要想实现对新数据库的impdp(导入)工作, 首先需要从老的数 ...

  9. 2019-3-16-win10-uwp-在-ItemsPanelTemplate-里面通过样式绑定-Orientation-显示方向

    title author date CreateTime categories win10 uwp 在 ItemsPanelTemplate 里面通过样式绑定 Orientation 显示方向 lin ...

  10. ionic js 滑动框ion-slide-box 滑动框是一个包含多页容器的组件,每页滑动或拖动切换

    ionic 滑动框 ion-slide-box 滑动框是一个包含多页容器的组件,每页滑动或拖动切换: 效果图如下: 用法 <ion-slide-box on-slide-changed=&quo ...