实现上图功能的具体代码:
生成一个用户控件,后面可以直接使用该用户控件。
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Selector.ascx.cs" Inherits="Controls_Selector" %>
<table>
<tr>
<td align="center">
<asp:Label ID="lblNotSelected" runat="server" Text="未选"></asp:Label></td>
<td>
</td>
<td align="center">
<asp:Label ID="lblSelected" runat="server" Text="已选"></asp:Label></td>
</tr>
<tr>
<td align="center">
<asp:ListBox ID="lvwUnSelected" runat="server" Rows="10" Width="150px"></asp:ListBox></td>
<td align="center">
<table style="height:100%">
<tr style="height:25%">
<td>
<asp:Button ID="btnSelAll" CssClass="button2" runat="server" Text="全选>>" OnClick="btnSelAll_Click" /></td>
</tr>
<tr style="height:25%">
<td>
<asp:Button ID="btnSelOne" CssClass="button2" runat="server" Text="选择>" OnClick="btnSelOne_Click"/></td>
</tr>
<tr style="height:25%">
<td>
<asp:Button ID="btnUnSelOne" CssClass="button2" runat="server" Text="<移除" OnClick="btnUnSelOne_Click" /></td>
</tr>
<tr style="height:25%">
<td>
<asp:Button ID="btnSelNone" CssClass="button2" runat="server" Text="<<不选" OnClick="btnSelNone_Click" /></td>
</tr>
</table>
</td>
<td align="center">
<asp:ListBox ID="lvwSelected" runat="server" Rows="10" Width="150px"></asp:ListBox></td>
</tr>
</table>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Controls_Selector : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 获取或设置数据源。
/// </summary>
public DataSet DataSource
{
get
{
if (lvwUnSelected.DataSource == null)
{
return null;
}
else if (lvwSelected.DataSource == null)
{
DataSet ds = new DataSet();
ds.Tables.Add((DataTable)lvwUnSelected.DataSource);
return ds;
}
else
{
DataSet ds = new DataSet();
ds.Tables.Add((DataTable)lvwUnSelected.DataSource);
ds.Tables.Add((DataTable)lvwSelected.DataSource);
return ds;
}
}
set
{
if (value.Tables.Count > 0)
{
lvwUnSelected.DataSource = value.Tables[0];
if (value.Tables.Count > 1)
{
lvwSelected.DataSource = value.Tables[1];
}
}
}
}
/// <summary>
/// 获取或设置未选中上方的文本。
/// </summary>
public string LabelUnSelected
{
get
{
return lblNotSelected.Text;
}
set
{
lblNotSelected.Text = value;
}
}
/// <summary>
/// 获取或设置选中上方的文本。
/// </summary>
public string LabelSelected
{
get
{
return lblSelected.Text;
}
set
{
lblSelected.Text = value;
}
}
/// <summary>
/// 获取或设置为列表项提供文本内容的数据源字段。
/// </summary>
public string DataTextField
{
get
{
return lvwUnSelected.DataTextField;
}
set
{
lvwUnSelected.DataTextField = value;
lvwSelected.DataTextField = value;
}
}
/// <summary>
/// 获取或设置为各列表项提供值的数据源字段。
/// </summary>
public string DataValueField
{
get
{
return lvwUnSelected.DataValueField;
}
set
{
lvwUnSelected.DataValueField = value;
lvwSelected.DataValueField = value;
}
}
/// <summary>
/// 获取未选中 ListBox (左)。
/// </summary>
public ListBox UnSelected
{
get
{
return lvwUnSelected;
}
}
/// <summary>
/// 获取选中 ListBox (右)。
/// </summary>
public ListBox Selected
{
get
{
return lvwSelected;
}
}
/// <summary>
/// 获取未被选中(左边 ListBox )的项。
/// </summary>
public ListItemCollection UnSelectedItems
{
get
{
return lvwUnSelected.Items;
}
}
/// <summary>
/// 获取被选中(右边 ListBox )的项。
/// </summary>
public ListItemCollection SelectedItems
{
get
{
return lvwSelected.Items;
}
}
/// <summary>
/// 获取新添加项的值。
/// </summary>
public ArrayList AddedItemValues
{
get
{
if (ViewState["AddedItems"]==null)
{
ViewState["AddedItems"] = new ArrayList();
}
return (ArrayList)ViewState["AddedItems"];
}
}
/// <summary>
/// 获取从已选择的数据移除项的值。
/// </summary>
public ArrayList RemovedItemValues
{
get
{
if (ViewState["RemovedItems"]==null)
{
ViewState["RemovedItems"] = new ArrayList();
}
return (ArrayList)ViewState["RemovedItems"];
}
}
/// <summary>
/// 获取或设置 ListBox 的 Css 样式。
/// </summary>
public string ListBoxCssClass
{
get
{
return lvwUnSelected.CssClass;
}
set
{
lvwUnSelected.CssClass = value;
lvwSelected.CssClass = value;
}
}
/// <summary>
/// 获取或设置 Button 的 Css 样式。
/// </summary>
public string ButtonCssClass
{
get
{
return btnSelAll.CssClass;
}
set
{
btnSelAll.CssClass = value;
btnSelNone.CssClass = value;
btnUnSelOne.CssClass = value;
btnSelOne.CssClass = value;
}
}
/// <summary>
/// 获取或设置 Button 的宽度。
/// </summary>
public Unit ButtonWidth
{
get
{
return btnUnSelOne.Width;
}
set
{
btnUnSelOne.Width = value;
btnSelAll.Width = value;
btnSelOne.Width = value;
btnSelNone.Width = value;
}
}
/// <summary>
/// 获取或设置 ListBox 的宽度。
/// </summary>
public Unit ListBoxWidth
{
get
{
return lvwUnSelected.Width;
}
set
{
lvwUnSelected.Width = value;
lvwSelected.Width = value;
}
}
/// <summary>
/// 获取或设置 ListBox 的行数。
/// </summary>
public int Rows
{
get
{
return lvwUnSelected.Rows;
}
set
{
lvwUnSelected.Rows = value;
lvwSelected.Rows = value;
}
}
/// <summary>
/// 获取或设置 ListBox 的选择模式。
/// </summary>
public ListSelectionMode SelectionMode
{
get
{
return lvwUnSelected.SelectionMode;
}
set
{
lvwUnSelected.SelectionMode = value;
lvwSelected.SelectionMode = value;
}
}
/// <summary>
/// 绑定数据源。(一般用于第一次绑定。)
/// </summary>
public new void DataBind()
{
AddedItemValues.Clear();
RemovedItemValues.Clear();
lvwUnSelected.DataBind();
lvwSelected.DataBind();
}
/// <summary>
/// 重新绑定数据源(右边的数据将不会消失)。
/// </summary>
public void Rebind()
{
if (lvwUnSelected.DataSource == null)
{
return;
}
DataTable dt = (DataTable)lvwUnSelected.DataSource;
string selectItems = "";
foreach (ListItem item in SelectedItems)
{
selectItems += "'" + item.Value + "',";
}
if (selectItems.Length > 0) // 去除多加的','
{
selectItems.Remove(selectItems.Length - 1, 1);
DataRow[] rows = dt.Select(DataValueField + " IN (" + selectItems + ")");
for (int i = 0; i < rows.Length; i++)
{
dt.Rows.Remove(rows[i]);
}
}
lvwUnSelected.DataBind();
}
#region 按钮事件处理。
/// <summary>
/// 选中左侧全部。
/// </summary>
protected void btnSelAll_Click(object sender, EventArgs e)
{
if (lvwSelected.SelectionMode == ListSelectionMode.Single&& lvwSelected.SelectedItem!=null)
{
lvwSelected.SelectedItem.Selected = false; // 不允许选择多个。不加入这段代码报错。
}
foreach (ListItem item in lvwUnSelected.Items)
{
lvwSelected.Items.Add(item);
if (RemovedItemValues.Contains(item.Value)) // 如果在 RemovedItems 中,则把 RemovedItems 删除当前元素;否则在 AddItems 中增加当前元素。
{
RemovedItemValues.Remove(item.Value);
}
else
{
AddedItemValues.Add(item.Value);
}
}
lvwUnSelected.Items.Clear();
}
/// <summary>
/// 选中左侧部分。
/// </summary>
protected void btnSelOne_Click(object sender, EventArgs e)
{
if (lvwSelected.SelectionMode == ListSelectionMode.Single && lvwSelected.SelectedItem != null)
{
lvwSelected.SelectedItem.Selected = false; // 不允许选择多个。不加入这段代码报错。
}
foreach (ListItem item in lvwUnSelected.Items)
{
if (item.Selected)
{
lvwSelected.Items.Add(item);
if (RemovedItemValues.Contains(item.Value)) // 如果在 RemovedItems 中,则把 RemovedItems 删除当前元素;否则在 AddItems 中增加当前元素。
{
RemovedItemValues.Remove(item.Value);
}
else
{
AddedItemValues.Add(item.Value);
}
}
}
for (int i = 0; i < lvwUnSelected.Items.Count; i++)
{
if (lvwUnSelected.Items[i].Selected)
{
lvwUnSelected.Items.RemoveAt(i);
i--;
}
}
}
/// <summary>
/// 选中右侧部分。
/// </summary>
protected void btnUnSelOne_Click(object sender, EventArgs e)
{
if (lvwUnSelected.SelectionMode == ListSelectionMode.Single && lvwUnSelected.SelectedItem != null)
{
lvwUnSelected.SelectedItem.Selected = false; // 不允许选择多个。不加入这段代码报错。
}
foreach (ListItem item in lvwSelected.Items)
{
if (item.Selected)
{
lvwUnSelected.Items.Add(item);
if (AddedItemValues.Contains(item.Value)) // 如果在 AddedItems 中,则把 AddedItems 删除当前元素;否则在 RemovedItems 中增加当前元素。
{
AddedItemValues.Remove(item.Value);
}
else
{
RemovedItemValues.Add(item.Value);
}
}
}
for (int i = 0; i < lvwSelected.Items.Count; i++)
{
if (lvwSelected.Items[i].Selected)
{
lvwSelected.Items.RemoveAt(i);
i--;
}
}
}
/// <summary>
/// 选中右侧全部。
/// </summary>
protected void btnSelNone_Click(object sender, EventArgs e)
{
if (lvwUnSelected.SelectionMode == ListSelectionMode.Single && lvwUnSelected.SelectedItem != null)
{
lvwUnSelected.SelectedItem.Selected = false; // 不允许选择多个。不加入这段代码报错。
}
foreach (ListItem item in lvwSelected.Items)
{
lvwUnSelected.Items.Add(item);
if (AddedItemValues.Contains(item.Value)) // 如果在 AddedItems 中,则把 AddedItems 删除当前元素;否则在 RemovedItems 中增加当前元素。
{
AddedItemValues.Remove(item.Value);
}
else
{
RemovedItemValues.Add(item.Value);
}
}
lvwSelected.Items.Clear();
}
#endregion
}
使用时:
前台:
<%@ Register Src="../UserControl/Selector.ascx" TagName="Selector" TagPrefix="uc1" %>
<div align="center">
<uc1:Selector ID="Selector1" runat="server" ButtonCssClass="button1"
LabelSelected="已选薪资项" LabelUnSelected="未选薪资项" Rows="20"/>
<table id="OperationButton" align="center" height="40">
<tr valign="middle">
<td>
<asp:Button ID="btnAllowModify" runat="server" CssClass="button1" Visible="false" Text="允许修改" /></td>
<td>
<asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" OnClientClick="return CheckPage();"/></td>
<td>
<asp:Button ID="btnReturn" runat="server" Text="返回" Visible="false"/></td>
</tr>
</table>
</div>
后台:
绑定两端的数据:
string sql1 = "xxxxx1";
string sql2 = "xxxx2";
DataTable table1 = dataAccess.GetDataTable(sql1).Copy();
DataTable table2 = dataAccess.GetDataTable(sql2).Copy();
table2.TableName = "table2";
System.Data.DataSet dataSource = new System.Data.DataSet();
dataSource.Tables.Add(table2);
dataSource.Tables.Add(table1);
Selector1.DataSource = dataSource;
Selector1.DataTextField = "c_itemdetail_name";
Selector1.DataValueField = "c_itemdetail_id";
Selector1.DataBind();
保存时:
使用新添加的项:
if (dataAccess.ExecuteSQL(sql) > 0 && Selector1.AddedItemValues.Count > 0)
{
string[] SelectedItem = ToValueStringHelper.ToValueString(Selector1.AddedItemValues).Split(',');
foreach (string item in SelectedItem)
{
sql = string.Format("insert into T_WAGEFULI_ITEMOFLIST(C_ITEMOFLIST_ID,C_ITEMOFLIST_ITEMID,C_ITEMOFLIST_LISTID) " +
" values (SEQ_T_WAGEFULI_ITEMOFLIST.Nextval,{0},{1})", item, ListID);
dataAccess.ExecuteSQL(sql);
}
}
使用移除的项:
if (Selector1.AddedItemValues.Count > 0 || Selector1.RemovedItemValues.Count > 0)
{
try
{
if (Selector1.RemovedItemValues.Count > 0) //删除移除的薪资项
{
string ItemList = ToValueStringHelper.ToValueString(Selector1.RemovedItemValues);
sqlRemove = string.Format("delete from t_wagefuli_itemoflis t t where t.c_itemoflist_listid = {0} and t.c_itemoflist_itemid in ({1})",
ListID, ItemList);
dataAccess.ExecuteSQL(sqlRemove);
}
if (Selector1.AddedItemValues.Count > 0) //添加新增的薪资项
{
string[] SelectedItem = ToValueStringHelper.ToValueString(Selector1.AddedItemValues).Split(',');
foreach (string item in SelectedItem)
{
sqlAdd = string.Format("insert into T_WAGEFULI_ITEMOFLIST(C_ITEMOFLIST_ID,C_ITEMOFLIST_ITEMID,C_ITEMOFLIST_LISTID) " +
" values (SEQ_T_WAGEFULI_ITEMOFLIST.Nextval,{0},{1})", item, ListID);
dataAccess.ExecuteSQL(sqlAdd);
}
}
}
catch (Exception er)
{
MessageBox.Show("操作过程发生异常,异常信息为:" + er.Message, this);
return;
}
finally
{
dataAccess = null;
}
Selector1.AddedItemValues.Clear();
Selector1.RemovedItemValues.Clear();
//Selector1.SelectedItems.Clear();
//Selector1.UnSelectedItems.Clear();
}