• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悠
閒

博客园    首页       联系   管理    订阅  订阅
关于Button控件的CommandName属性用法的一个实例

前台截图:

例子中有两个ListBox控件、四个Button控件和一个Label控件。数据显示需要绑定数据库。

例子要实现的功能很简单:利用Button控件的CommandName属性来操作两个ListBox控件中数据的移动。

 

前台代码:

MyListBox.aspx
<%@ 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="6">
                    
<asp:ListBox ID="SourceList" runat="server" Height="160px" Width="200px">
                    
</asp:ListBox>
                
</td>
                
<td class="style3">
                    
&nbsp;</td>
                
<td class="style4" rowspan="6">
                    
<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>

 

 

后台代码:

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 > -1)
                {
                    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 > -1)
                {
                    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 = -1;
        DirectList.SelectedIndex 
= -1;
    }
}

 

posted on 2009-11-24 09:51  悠閒  阅读(12065)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3