首页 > 代码库 > 关于Button控件的CommandName属性用法的一个实例
关于Button控件的CommandName属性用法的一个实例
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 using System.Data.SqlClient; 9 using System.Configuration; 10 11 public partial class MyListBox : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 if (!IsPostBack) 16 { 17 //让按钮事件和AddAndDelete_Command方法建立关联 18 this.btnAddOne.Command += new CommandEventHandler(this.AddAndDelete_Command); 19 this.btnAddAll.Command += new CommandEventHandler(this.AddAndDelete_Command); 20 this.btnDeleteOne.Command += new CommandEventHandler(this.AddAndDelete_Command); 21 this.btnDeleteAll.Command += new CommandEventHandler(this.AddAndDelete_Command); 22 23 //另一种建立关联的写法 24 //this.btnAddOne.Click += new EventHandler(this.AddAndDelete_Command); 25 //this.btnAddAll.Click += new EventHandler(this.AddAndDelete_Command); 26 //this.btnDeleteOne.Click += new EventHandler(this.AddAndDelete_Command); 27 //this.btnDeleteAll.Click += new EventHandler(this.AddAndDelete_Command); 28 29 //加载并显示数据 30 GetUserName(); 31 } 32 } 33 34 //加载数据,绑定到SourceList控件 35 private void GetUserName() 36 { 37 //清空ListBox控件的所有数据项 38 SourceList.Items.Clear(); 39 40 //连接、读取数据,并把数据绑定到SourceList控件 41 string con = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString; 42 SqlConnection myCon = new SqlConnection(con); 43 string cmdText = "SELECT UI_UserID,UI_UserName FROM tb_UserInfo ORDER BY UI_UserID"; 44 SqlCommand myCom = new SqlCommand(cmdText, myCon); 45 46 myCon.Open(); 47 SqlDataReader myReader = myCom.ExecuteReader(); 48 49 while (myReader.Read()) 50 { 51 SourceList.Items.Add(new ListItem(myReader["UI_UserName"].ToString(), myReader["UI_UserID"].ToString())); 52 } 53 54 myReader.Close(); 55 myCon.Close(); 56 } 57 58 //根据按钮控件的CommandName属性进行判断而进行不同的操作 59 public void AddAndDelete_Command(object sender, System.EventArgs e) 60 { 61 string commandName = ((Button)sender).CommandName; 62 63 switch (commandName) 64 { 65 case "addOne": 66 if (SourceList.SelectedIndex > -1) 67 { 68 DirectList.Items.Add(SourceList.SelectedItem); 69 lblSucessMessage.Visible = false; 70 } 71 else 72 { 73 lblSucessMessage.Visible = true; 74 } 75 break; 76 case "addAll": 77 lblSucessMessage.Visible = false; 78 DirectList.Items.Clear(); 79 foreach (ListItem item in SourceList.Items) 80 { 81 DirectList.Items.Add(item); 82 } 83 break; 84 case "deleteOne": 85 if (DirectList.SelectedIndex > -1) 86 { 87 DirectList.Items.Remove(DirectList.SelectedItem); 88 lblSucessMessage.Visible = false; 89 } 90 else 91 { 92 lblSucessMessage.Visible = true; 93 } 94 break; 95 case "deleteAll": 96 lblSucessMessage.Visible = false; 97 DirectList.Items.Clear(); 98 break; 99 default: break;100 }101 102 //清空两个列表控件的选项103 SourceList.SelectedIndex = -1;104 DirectList.SelectedIndex = -1;105 }106 }
前台显示如图:
关于Button控件的CommandName属性用法的一个实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。