前台截图:
例子中有两个ListBox控件、四个Button控件和一个Label控件。数据显示需要绑定数据库。
例子要实现的功能很简单:利用Button控件的CommandName属性来操作两个ListBox控件中数据的移动。
前台代码:
MyListBox.aspx
后台代码:
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;
}
}
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;
}
}