关于Button控件的CommandName属性用法的一个实例
2024-10-07 21:41:21
注:本文分享于悠闲的博客,地址: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">
</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=">" CommandName="addOne"
Height="25px" onclick="AddAndDelete_Command" Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnAddAll" runat="server" Text=">>" CommandName="addAll"
Height="25px" onclick="AddAndDelete_Command" Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnDeleteOne" runat="server" Text="<"
CommandName="deleteOne" Height="25px" onclick="AddAndDelete_Command"
Width="50px" />
</td>
</tr>
<tr>
<td class="style3" align="center">
<asp:Button ID="btnDeleteAll" runat="server" Text="<<"
CommandName="deleteAll" Height="25px" onclick="AddAndDelete_Command"
Width="50px" />
</td>
</tr>
<tr>
<td class="style3">
</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 = -;
}
}
最新文章
- 关于我-dinphy简介
- LabVIEW 吸星大法 - 看见的好东西都是我的(下篇)
- lintcode 滑动窗口的最大值(双端队列)
- Android 动态创建Fragment
- 将u盘的文件复制到虚拟机上的linux系统上面—》文件挂载(文字+图解)
- WinForm程序打包说明
- 【网络——Linux】——IPMI详细介绍【转】
- itextsharp去掉PDF加密
- JAVA HttpsURLConnection 忽略对SSL valid 的验证
- power shell upload file to azure storage
- POJ 3280 Cheapest Palindrome(水题)
- maven安装和配置及创建maven项目
- ServletConfig、ServletContext 的应用
- window 编译lua 5.3
- [No0000120]Python教程3/9-第一个Python程序
- gitblit.cmd运行自动关闭
- [svc]traceroute(udp+icmp)&;tracert(icmp)原理
- haproxy-1.7.7 源码安装
- JDK 12又来了,我学不动了...
- XML_Qt_资料
热门文章
- delphi mysql
- NX二次开发-UFUN设置环境变量UF_set_variable
- (转) MySQL中索引的限制
- 李宏毅机器学习课程---4、Gradient Descent (如何优化 )
- nc命令官方Demo实例
- class3_Entry &; Text 输入和文本框
- 1、linux常用命令的英文单词缩写
- eclispe 创建maven 项目:Could not resolve archetype org.apache.maven.archetypes
- 1.springboot+ActiveMQ
- dubbo视频分享