📄 multilistbox.cs
字号:
/// 绑定数据源
/// </summary>
public new void DataBind()
{
PerformanceData(FirstListBox);
PerformanceData(SecondListBox);
}
/// <summary>
/// 将DataSource转换成指定的Items
/// </summary>
/// <param name="listItem"></param>
protected void PerformanceData(MultiListBoxItem listItem)
{
object dataSource = listItem.DataSource;
if (dataSource != null)
{
bool flag_display = false;
bool flag_format = false;
string propName = this.DataTextField;
string dataValueField = this.DataValueField;
string format = this.DataTextFormatString;
ICollection data = dataSource as ICollection;
if (data != null)
{
listItem.Items.Capacity = listItem.Items.Count + data.Count;
}
if (propName.Length != 0 || dataValueField.Length != 0)
flag_display = true;
if (this.DataTextFormatString.Length != 0)
flag_format = true;
foreach (object obj in data)
{
ListItem item = new ListItem();
//自定义显示TextField,ValueField
if (flag_display)
{
if (propName.Length > 0)
{
item.Text = DataBinder.GetPropertyValue(obj, propName).ToString();
}
if (dataValueField.Length > 0)
{
item.Value = DataBinder.GetPropertyValue(obj, dataValueField).ToString();
}
}
else
{
if (flag_format)
{
item.Text = string.Format(CultureInfo.CurrentCulture, format, new object[] { obj });
}
else
{
item.Text = obj.ToString();
}
}
listItem.Items.Add(item);
}
}
}
#endregion
#region OnPreRender
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
if (!Page.ClientScript.IsClientScriptBlockRegistered("MultiListBox-JavaScriptKey"))
{
//注册脚本
string jsResource = Page.ClientScript.GetWebResourceUrl(typeof(MultiListBox), "FrameWork.WebControls.Resource.MultiListBox.js");
Page.ClientScript.RegisterClientScriptInclude("MultiListBox-JavaScriptKey", jsResource);
//注册隐藏域
Page.ClientScript.RegisterHiddenField(this.HFItemsAdded, "");
Page.ClientScript.RegisterHiddenField(this.HFItemsRemoved, "");
}
}
#endregion
#region IPostBackDataHandler
/// <summary>
///
/// </summary>
/// <param name="postDataKey"></param>
/// <param name="postCollection"></param>
/// <returns></returns>
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
bool resultValueFlag = false;
//移除指定ListItem,并需要添加了Left ListBox列表框中
string itemsRemoved = postCollection[this.ClientID + "_REMOVED"];
string[] itemsRemovedCol = itemsRemoved.Split(',');
if (itemsRemovedCol != null)
{
if (itemsRemovedCol.Length > 0 && itemsRemovedCol[0] != "")
{
for (int i = 0; i < itemsRemovedCol.Length; i++)
{
string[] itemsRemoveItems = itemsRemovedCol[i].Split('|');
ListItem item = this.SecondListBox.Items.FindByValue(itemsRemoveItems[1]);
if (item != null)
{
this.SecondListBox.Items.Remove(item);
}
item = this.FirstListBox.Items.FindByValue(itemsRemoveItems[1]);
if (item == null)
{
this.FirstListBox.Items.Add(new ListItem(itemsRemoveItems[0], itemsRemoveItems[1]));
}
resultValueFlag = true;
}
}
}
//从客户端添加指定的ListItem
string itemsAdded = postCollection[this.ClientID + "_ADDED"];
string[] itemsAddedCol = itemsAdded.Split(',');
if (itemsAddedCol != null)
{
if (itemsAddedCol.Length > 0 && itemsAddedCol[0] != "")
{
int counter = -1;
for (int i = 0; i < itemsAddedCol.Length; i++)
{
string[] itemsAddItems = itemsAddedCol[i].Split('|');
ListItem item = this.SecondListBox.Items.FindByValue(itemsAddItems[1]);
if (item == null)
{
this.SecondListBox.Items.Add(new ListItem(itemsAddItems[0], itemsAddItems[1]));
counter += 1;
}
item = this.FirstListBox.Items.FindByValue(itemsAddItems[1]);
if (item != null)
{
this.FirstListBox.Items.Remove(item);
}
}
resultValueFlag = counter > -1 ? true : false;
}
}
//从客户端中移除指定的ListItem
return resultValueFlag;
}
#endregion
#region Functionality
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="multiListItem"></param>
/// <param name="name"></param>
/// <param name="jsDbClick"></param>
protected void RenderOptionsContents(HtmlTextWriter writer, MultiListBoxItem multiListItem, string name, string jsDbClick)
{
ListItemCollection items = multiListItem.Items;
writer.AddAttribute("name", this.ClientID + "_" + name);
writer.AddAttribute("id", this.ClientID + "_" + name);
writer.AddAttribute("size", this.Rows.ToString());
writer.AddAttribute("onDblClick", jsDbClick);
if (SelectionMode == ListSelectionMode.Multiple)
writer.AddAttribute(HtmlTextWriterAttribute.Multiple, "multiple");
//输出CSS定义
if (multiListItem.StyleSheet != null)
{
StringBuilder sb = new StringBuilder();
string sheetValue = multiListItem.StyleSheet.Width;
if (sheetValue != null && sheetValue != "")
sb.AppendFormat("width:{0};", sheetValue);
sheetValue = multiListItem.StyleSheet.Height;
if (sheetValue != null && sheetValue != "")
sb.AppendFormat("height:{0};", sheetValue);
writer.AddAttribute("style", sb.ToString());
}
writer.RenderBeginTag(HtmlTextWriterTag.Select);
int count = items.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
ListItem item = items[i];
writer.AddAttribute("value", item.Value, true);
writer.RenderBeginTag(HtmlTextWriterTag.Option);
writer.Write(item.Text);
writer.RenderEndTag();
}
}
writer.RenderEndTag();
}
/// <summary>
/// 显示操作图标
/// </summary>
/// <param name="writer"></param>
protected void RenderMultiIcon(HtmlTextWriter writer)
{
//输出Div
writer.RenderBeginTag(HtmlTextWriterTag.Div);
RenderImageContents(writer, "moveAllRight.gif", "multiList.transferAllLeft();", "_AllRight");//全部右移
RenderImageContents(writer, "moveRight.gif", "multiList.transferRight();", "_Right");
RenderImageContents(writer, "moveLeft.gif", "multiList.transferLeft();", "_Left");
RenderImageContents(writer, "moveAllLeft.gif", "multiList.transferAllRight();", "_AllLeft");
writer.RenderEndTag();
}
private string GetWebResourceUrl(string img)
{
return Page.ClientScript.GetWebResourceUrl(typeof(MultiListBox), "FrameWork.WebControls.Resource." + img);
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="imgFileName"></param>
/// <param name="jsClickName"></param>
/// <param name="imgClientName"></param>
private void RenderImageContents(HtmlTextWriter writer, string imgFileName, string jsClickName, string imgClientName)
{
writer.RenderBeginTag(HtmlTextWriterTag.P);
writer.AddAttribute(HtmlTextWriterAttribute.Src, GetWebResourceUrl(imgFileName));
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + imgClientName);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID + imgClientName);
writer.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, jsClickName);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.RenderEndTag();
}
#endregion
}
/// <summary>
/// Multi-ListItem
/// </summary>
public class MultiListBoxItem
{
#region Fields
private ListItemCollection items;
private object _dataSource = null;
private string _style = string.Empty;
private MultiListBoxStyle _styleSheet = new MultiListBoxStyle();
#endregion
#region Propertity
/// <summary>
/// 获取列表控件项的集合
/// </summary>
[Browsable(false)]
public ListItemCollection Items
{
get
{
if (items == null)
{
items = new ListItemCollection();
}
return items;
}
}
/// <summary>
/// 获取或设置列表控件的数据源
/// </summary>
[Browsable(false)]
public object DataSource
{
get
{
ValidateDataSource();
return this._dataSource;
}
set
{
this._dataSource = value;
}
}
/// <summary>
///
/// </summary>
[Browsable(true), Description("Css样式名称")]
public string Style
{
get { return _style; }
set { _style = value; }
}
/// <summary>
///
/// </summary>
[Browsable(true), Description("Css样式定义,包括高度,宽度。如果定义了<Style>样式名称,该属性可以忽略")]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public MultiListBoxStyle StyleSheet
{
get { return _styleSheet; }
set { _styleSheet = value; }
}
#endregion
/// <summary>
/// 验证数据源
/// </summary>
protected void ValidateDataSource()
{
if (_dataSource != null && !(_dataSource is ICollection) && !(_dataSource is IEnumerable) && !(_dataSource is IListSource))
{
throw new InvalidOperationException("DataBoundControl_InvalidDataSourceType");
}
}
}
/// <summary>
///
/// </summary>
public class Reflector
{
/// <summary>
///
/// </summary>
/// <param name="items"></param>
/// <param name="methodName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static object InvokeMethod(ListItemCollection items, string methodName, object[] parameters)
{
Type type = items.GetType();
FieldInfo fieldInfo = type.GetField("saveAll", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Default);
fieldInfo.SetValue(items, (object)true);//
MethodInfo methodInfo = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Default | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (methodInfo != null)
{
return methodInfo.Invoke(items, parameters);
}
return null;
}
}
/// <summary>
///
/// </summary>
public class MultiListBoxStyle
{
#region Fields
private string _width;
private string _height;
#endregion
#region Propertity
/// <summary>
/// 宽
/// </summary>
public string Width
{
get { return _width; }
set { _width = value; }
}
/// <summary>
/// 高
/// </summary>
public string Height
{
get { return _height; }
set { _height = value; }
}
#endregion
}
[AttributeUsage(AttributeTargets.All)]
internal sealed class WebCategoryAttribute : CategoryAttribute
{
/// <summary>
/// 使用指定的<paramref name="category"/>名称
/// </summary>
/// <param name="category"></param>
internal WebCategoryAttribute(string category)
: base(category)
{
}
public override object TypeId
{
get
{
return typeof(CategoryAttribute);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -