MultiSelector.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:13k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Collections;
- using System.Web;
- using System.Web.UI;
- using System.Drawing;
- using System.ComponentModel;
- using System.Drawing.Design;
- using System.Web.UI.WebControls;
- using System.Collections.Generic;
- [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
- namespace OThinker.H3.WorkSheet
- {
- /// <summary>
- /// MultiSelector 的摘要说明。
- /// </summary>
- [ToolboxBitmap(typeof(MultiSelector), "OThinker.H3.WorkSheet.MultiSelector.bmp")]
- [ToolboxData("<{0}:MultiSelector runat=server></{0}:MultiSelector>")]
- public class MultiSelector : System.Web.UI.WebControls.WebControl
- {
- private ListBox _OptionList = new ListBox();
- /// <summary>
- /// 用于存储多值
- /// </summary>
- public ListBox OptionList
- {
- get
- {
- return this._OptionList;
- }
- }
- private ListBox _SelectionList = new ListBox();
- /// <summary>
- /// 用于存储多值
- /// </summary>
- public ListBox SelectionList
- {
- get
- {
- return this._SelectionList;
- }
- }
- private TextBox _SelectionText = new TextBox();
- /// <summary>
- /// 用于存储用户ID
- /// </summary>
- public TextBox SelectionText
- {
- get
- {
- return this._SelectionText;
- }
- }
- private char _Separator = ';';
- /// <summary>
- /// Value的分隔符
- /// </summary>
- public char Separator
- {
- get
- {
- return this._Separator;
- }
- set
- {
- this._Separator = value;
- }
- }
- public MultiSelector()
- {
- this.Controls.Add(this.OptionList);
- this.Controls.Add(this.SelectionList);
- this.Controls.Add(this.SelectionText);
- }
- #region 可选的名称和值
- private List<string> OptionNameList = new List<string>();
- public string[] OptionNames
- {
- get
- {
- return this.OptionNameList.ToArray();
- }
- set
- {
- if (value != null)
- {
- this.OptionNameList = new List<string>(value);
- }
- else
- {
- this.OptionNameList = new List<string>();
- }
- this.UpdateOptionList();
- }
- }
- private List<string> OptionValueList = new List<string>();
- public string[] OptionValues
- {
- get
- {
- return this.OptionValueList.ToArray();
- }
- set
- {
- if (value != null)
- {
- this.OptionValueList = new List<string>(value);
- }
- else
- {
- this.OptionValueList = new List<string>();
- }
- this.UpdateOptionList();
- }
- }
- public string GetOptionName(string Value)
- {
- int count = 0;
- for (count = 0; count < this.OptionValueList.Count; count++)
- {
- if (Value == this.OptionValueList[count])
- {
- break;
- }
- }
- if (count == this.OptionValueList.Count)
- {
- return Value;
- }
- else if (count >= this.OptionNameList.Count)
- {
- return Value;
- }
- else
- {
- return this.OptionNameList[count];
- }
- }
- private void UpdateOptionList()
- {
- this.OptionList.Items.Clear();
- for (int count = 0; count < this.OptionValueList.Count; count++)
- {
- string value = this.OptionValueList[count];
- string name = null;
- if (count < this.OptionNameList.Count)
- {
- name = this.OptionNameList[count];
- }
- this.OptionList.Items.Add(new ListItem(name, value));
- }
- }
- #endregion
- #region 加载选中的值
- public string[] SelectedItems
- {
- get
- {
- string text = this.SelectionText.Text;
- if (text == null)
- {
- return null;
- }
- else
- {
- string[] items = text.Split(new char[] { this.Separator });
- List<string> list = new List<string>();
- if (items != null)
- {
- foreach (string item in items)
- {
- if (item != null && item != "")
- {
- list.Add(item);
- }
- }
- }
- return list.ToArray();
- }
- }
- set
- {
- this.SelectionText.Text = null;
- this.SelectionList.Items.Clear();
- if (value == null)
- {
- return;
- }
- foreach (string itemValue in value)
- {
- string itemName = this.GetOptionName(itemValue);
- this.SelectionText.Text += (itemValue + ";");
- this.SelectionList.Items.Add(new ListItem(itemName, itemValue));
- }
- }
- }
- #endregion
- protected override void Render(HtmlTextWriter writer)
- {
- // 计算高度和宽度
- double listWidth = (this.Width.Value - 16) / 2;
- if (listWidth < 0)
- {
- listWidth = 0;
- }
- double listHeight = this.Height.Value;
- string styleAttrbites = null;
- if (this.Width.Type == UnitType.Percentage)
- {
- styleAttrbites = "width:100%;";
- }
- else
- {
- this.SelectionList.Width = new Unit(listWidth);
- this.OptionList.Width = new Unit(listWidth);
- }
- if (this.Height.Type == UnitType.Percentage)
- {
- styleAttrbites += "height:100%;";
- }
- else
- {
- this.SelectionList.Height = new Unit(listHeight);
- this.OptionList.Height = new Unit(listHeight);
- }
- if (styleAttrbites != null)
- {
- this.SelectionList.Attributes.Add("style", styleAttrbites);
- this.OptionList.Attributes.Add("style", styleAttrbites);
- }
- writer.Write("<TABLE WIDTH="" + this.Width + "" HEIGHT="" + this.Height + "" cellpadding="0" cellspacing="0" border="0">");
- writer.Write("<TR>");
- writer.Write("<TD>");
- this.OptionList.RenderControl(writer);
- writer.Write("</TD>");
- writer.Write("<TD>");
- // 如果可编辑,则显示编辑按钮
- if (this.Enabled)
- {
- string addScript =
- // 获得选中的值
- "var optionListId='" + this.OptionList.ClientID + "';" + System.Environment.NewLine +
- "var optionList = document.getElementById(optionListId);" + System.Environment.NewLine +
- "var selectedValue; " +
- "var selectedName;" + System.Environment.NewLine +
- "for (i = optionList.length - 1; i >= 0; i--)" + System.Environment.NewLine +
- "{" + System.Environment.NewLine +
- " if (optionList.options[i].selected == true)" + System.Environment.NewLine +
- " {" + System.Environment.NewLine +
- " selectedValue=optionList.options[i].value;" + System.Environment.NewLine +
- " selectedName=optionList.options[i].text;" + System.Environment.NewLine +
- " }" + System.Environment.NewLine +
- "}" + System.Environment.NewLine +
- // 检查是否重复
- "var selectedListId='" + this.SelectionList.ClientID + "';" + System.Environment.NewLine +
- "var selectedList = document.getElementById(selectedListId);" + System.Environment.NewLine +
- "for (i = selectedList.length - 1; i >= 0; i--)" + System.Environment.NewLine +
- "{" + System.Environment.NewLine +
- " if (selectedList.options[i].value == selectedValue)" + System.Environment.NewLine +
- " {" + System.Environment.NewLine +
- " alert('该项已经存在');" + System.Environment.NewLine +
- " return;" + System.Environment.NewLine +
- " }" +
- "}" +
- // 添加到选中列表中
- "var nodeValue = '<option value=' + selectedValue + '>';" +
- "node = document.createElement(nodeValue);" +
- "text = document.createTextNode(selectedName);" +
- "node.appendChild(text);" +
- "selectedList.appendChild(node);" +
- "var selectedTextId = '" + this.SelectionText.ClientID + "';" +
- "var selectedText = document.getElementById(selectedTextId);" +
- "selectedText.value = selectedText.value + selectedValue + '" + this.Separator.ToString() + "';";
- writer.Write("<A onclick="javascript:" + addScript + ";" style="CURSOR: hand" >></A>");
- writer.Write("<BR>");
- string clearScript =
- "var listId='" + this.SelectionList.ClientID + "';" + System.Environment.NewLine +
- "var selectedIdId='" + this.SelectionText.ClientID + "';" + System.Environment.NewLine +
- "var objlist=document.getElementById(listId);" + System.Environment.NewLine +
- "var removedId;" + System.Environment.NewLine +
- "if(objlist.length != null && objlist.length != 0)" +
- "{" +
- "for(i=objlist.length-1;i>=0;i--)" + System.Environment.NewLine +
- "{" + System.Environment.NewLine +
- "if(objlist.options[i].selected == true)" + System.Environment.NewLine +
- "{" + System.Environment.NewLine +
- "removedId = objlist.options[i].value;" + System.Environment.NewLine +
- "objlist.remove(i);" + System.Environment.NewLine +
- "}" + System.Environment.NewLine +
- "}" + System.Environment.NewLine +
- "}" +
- "if(removedId == null || removedId == '')" + System.Environment.NewLine +
- "{" +
- "alert('没有选中成员');" + System.Environment.NewLine +
- "}" +
- "else" +
- "{" + System.Environment.NewLine +
- // 从ID文本中删除
- @"var selectedUsers = document.getElementById(selectedIdId).value;" + System.Environment.NewLine +
- @"var start = selectedUsers.indexOf(removedId);
- var preStr = selectedUsers.substring(0, start);
- var postStart = start + removedId.length + 1;
- var postEnd = selectedUsers.length;
- var postStr = selectedUsers.substring(postStart, postEnd);
- document.getElementById(selectedIdId).value = preStr + postStr;
- }";
- writer.Write("<A onclick="javascript:" + clearScript + "" style="CURSOR: hand" ><</A>");
- }
- writer.Write("</TD>");
- writer.Write("<TD>");
- this.SelectionText.Attributes.Add("style", "display:none;");
- this.SelectionList.RenderControl(writer);
- this.SelectionText.RenderControl(writer);
- writer.Write("</TD>");
- writer.Write("</TR>");
- writer.Write("</TABLE>");
- }
- }
- }