MultiSelector.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:13k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.Drawing.Design;
  8. using System.Web.UI.WebControls;
  9. using System.Collections.Generic;
  10. [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
  11. namespace OThinker.H3.WorkSheet
  12. {
  13.     /// <summary>
  14.     /// MultiSelector 的摘要说明。
  15.     /// </summary>
  16.     [ToolboxBitmap(typeof(MultiSelector), "OThinker.H3.WorkSheet.MultiSelector.bmp")]
  17.     [ToolboxData("<{0}:MultiSelector runat=server></{0}:MultiSelector>")]
  18.     public class MultiSelector : System.Web.UI.WebControls.WebControl
  19.     {
  20.         private ListBox _OptionList = new ListBox();
  21.         /// <summary>
  22.         /// 用于存储多值
  23.         /// </summary>
  24.         public ListBox OptionList
  25.         {
  26.             get
  27.             {
  28.                 return this._OptionList;
  29.             }
  30.         }
  31.         private ListBox _SelectionList = new ListBox();
  32.         /// <summary>
  33.         /// 用于存储多值
  34.         /// </summary>
  35.         public ListBox SelectionList
  36.         {
  37.             get
  38.             {
  39.                 return this._SelectionList;
  40.             }
  41.         }
  42.         private TextBox _SelectionText = new TextBox();
  43.         /// <summary>
  44.         /// 用于存储用户ID
  45.         /// </summary>
  46.         public TextBox SelectionText
  47.         {
  48.             get
  49.             {
  50.                 return this._SelectionText;
  51.             }
  52.         }
  53.         private char _Separator = ';';
  54.         /// <summary>
  55.         /// Value的分隔符
  56.         /// </summary>
  57.         public char Separator
  58.         {
  59.             get
  60.             {
  61.                 return this._Separator;
  62.             }
  63.             set
  64.             {
  65.                 this._Separator = value;
  66.             }
  67.         }
  68.         public MultiSelector()
  69.         {
  70.             this.Controls.Add(this.OptionList);
  71.             this.Controls.Add(this.SelectionList);
  72.             this.Controls.Add(this.SelectionText);
  73.         }
  74.         #region 可选的名称和值
  75.         private List<string> OptionNameList = new List<string>();
  76.         public string[] OptionNames
  77.         {
  78.             get
  79.             {
  80.                 return this.OptionNameList.ToArray();
  81.             }
  82.             set
  83.             {
  84.                 if (value != null)
  85.                 {
  86.                     this.OptionNameList = new List<string>(value);
  87.                 }
  88.                 else
  89.                 {
  90.                     this.OptionNameList = new List<string>();
  91.                 }
  92.                 this.UpdateOptionList();
  93.             }
  94.         }
  95.         private List<string> OptionValueList = new List<string>();
  96.         public string[] OptionValues
  97.         {
  98.             get
  99.             {
  100.                 return this.OptionValueList.ToArray();
  101.             }
  102.             set
  103.             {
  104.                 if (value != null)
  105.                 {
  106.                     this.OptionValueList = new List<string>(value);
  107.                 }
  108.                 else
  109.                 {
  110.                     this.OptionValueList = new List<string>();
  111.                 }
  112.                 this.UpdateOptionList();
  113.             }
  114.         }
  115.         public string GetOptionName(string Value)
  116.         {
  117.             int count = 0;
  118.             for (count = 0; count < this.OptionValueList.Count; count++)
  119.             {
  120.                 if (Value == this.OptionValueList[count])
  121.                 {
  122.                     break;
  123.                 }
  124.             }
  125.             if (count == this.OptionValueList.Count)
  126.             {
  127.                 return Value;
  128.             }
  129.             else if (count >= this.OptionNameList.Count)
  130.             {
  131.                 return Value;
  132.             }
  133.             else
  134.             {
  135.                 return this.OptionNameList[count];
  136.             }
  137.         }
  138.         private void UpdateOptionList()
  139.         {
  140.             this.OptionList.Items.Clear();
  141.             for (int count = 0; count < this.OptionValueList.Count; count++)
  142.             {
  143.                 string value = this.OptionValueList[count];
  144.                 string name = null;
  145.                 if (count < this.OptionNameList.Count)
  146.                 {
  147.                     name = this.OptionNameList[count];
  148.                 }
  149.                 this.OptionList.Items.Add(new ListItem(name, value));
  150.             }
  151.         }
  152.         #endregion
  153.         #region 加载选中的值
  154.         public string[] SelectedItems
  155.         {
  156.             get
  157.             {
  158.                 string text = this.SelectionText.Text;
  159.                 if (text == null)
  160.                 {
  161.                     return null;
  162.                 }
  163.                 else
  164.                 {
  165.                     string[] items = text.Split(new char[] { this.Separator });
  166.                     List<string> list = new List<string>();
  167.                     if (items != null)
  168.                     {
  169.                         foreach (string item in items)
  170.                         {
  171.                             if (item != null && item != "")
  172.                             {
  173.                                 list.Add(item);
  174.                             }
  175.                         }
  176.                     }
  177.                     return list.ToArray();
  178.                 }
  179.             }
  180.             set
  181.             {
  182.                 this.SelectionText.Text = null;
  183.                 this.SelectionList.Items.Clear();
  184.                 if (value == null)
  185.                 {
  186.                     return;
  187.                 }
  188.                 foreach (string itemValue in value)
  189.                 {
  190.                     string itemName = this.GetOptionName(itemValue);
  191.                     this.SelectionText.Text += (itemValue + ";");
  192.                     this.SelectionList.Items.Add(new ListItem(itemName, itemValue));
  193.                 }
  194.             }
  195.         }
  196.         #endregion
  197.         protected override void Render(HtmlTextWriter writer)
  198.         {
  199.             // 计算高度和宽度
  200.             double listWidth = (this.Width.Value - 16) / 2;
  201.             if (listWidth < 0)
  202.             {
  203.                 listWidth = 0;
  204.             }
  205.             double listHeight = this.Height.Value;
  206.             string styleAttrbites = null;
  207.             if (this.Width.Type == UnitType.Percentage)
  208.             {
  209.                 styleAttrbites = "width:100%;";
  210.             }
  211.             else
  212.             {
  213.                 this.SelectionList.Width = new Unit(listWidth);
  214.                 this.OptionList.Width = new Unit(listWidth);
  215.             }
  216.             if (this.Height.Type == UnitType.Percentage)
  217.             {
  218.                 styleAttrbites += "height:100%;";
  219.             }
  220.             else
  221.             {
  222.                 this.SelectionList.Height = new Unit(listHeight);
  223.                 this.OptionList.Height = new Unit(listHeight);
  224.             }
  225.             if (styleAttrbites != null)
  226.             {
  227.                 this.SelectionList.Attributes.Add("style", styleAttrbites);
  228.                 this.OptionList.Attributes.Add("style", styleAttrbites);
  229.             }
  230.             writer.Write("<TABLE WIDTH="" + this.Width + "" HEIGHT="" + this.Height + "" cellpadding="0" cellspacing="0" border="0">");
  231.             writer.Write("<TR>");
  232.             writer.Write("<TD>");
  233.             this.OptionList.RenderControl(writer);
  234.             writer.Write("</TD>");
  235.             writer.Write("<TD>");
  236.             // 如果可编辑,则显示编辑按钮
  237.             if (this.Enabled)
  238.             {
  239.                 string addScript =
  240.                     // 获得选中的值
  241.                     "var optionListId='" + this.OptionList.ClientID + "';" + System.Environment.NewLine +
  242.                     "var optionList = document.getElementById(optionListId);" + System.Environment.NewLine +
  243.                     "var selectedValue; " +
  244.                     "var selectedName;" + System.Environment.NewLine +
  245.                     "for (i = optionList.length - 1; i >= 0; i--)" + System.Environment.NewLine +
  246.                     "{" + System.Environment.NewLine +
  247.                     "   if (optionList.options[i].selected == true)" + System.Environment.NewLine +
  248.                     "   {" + System.Environment.NewLine +
  249.                     "       selectedValue=optionList.options[i].value;" + System.Environment.NewLine +
  250.                     "       selectedName=optionList.options[i].text;" + System.Environment.NewLine +
  251.                     "   }" + System.Environment.NewLine +
  252.                     "}" + System.Environment.NewLine +
  253.                     // 检查是否重复
  254.                     "var selectedListId='" + this.SelectionList.ClientID + "';" + System.Environment.NewLine +
  255.                     "var selectedList = document.getElementById(selectedListId);" + System.Environment.NewLine +
  256.                     "for (i = selectedList.length - 1; i >= 0; i--)" + System.Environment.NewLine +
  257.                     "{" + System.Environment.NewLine +
  258.                     "   if (selectedList.options[i].value == selectedValue)" + System.Environment.NewLine +
  259.                     "   {" + System.Environment.NewLine +
  260.                     "       alert('该项已经存在');" + System.Environment.NewLine +
  261.                     "       return;" + System.Environment.NewLine +
  262.                     "   }" +
  263.                     "}" +
  264.                     // 添加到选中列表中
  265.                     "var nodeValue = '<option value=' + selectedValue + '>';" +
  266.                     "node = document.createElement(nodeValue);" +
  267.                     "text = document.createTextNode(selectedName);" +
  268.                     "node.appendChild(text);" +
  269.                     "selectedList.appendChild(node);" +
  270.                     "var selectedTextId = '" + this.SelectionText.ClientID + "';" +
  271.                     "var selectedText = document.getElementById(selectedTextId);" +
  272.                     "selectedText.value = selectedText.value + selectedValue + '" + this.Separator.ToString() + "';";
  273.                 writer.Write("<A onclick="javascript:" + addScript + ";" style="CURSOR: hand" >&gt;</A>");
  274.                 writer.Write("<BR>");
  275.                 string clearScript =
  276.                         "var listId='" + this.SelectionList.ClientID + "';" + System.Environment.NewLine +
  277.                         "var selectedIdId='" + this.SelectionText.ClientID + "';" + System.Environment.NewLine +
  278.                         "var objlist=document.getElementById(listId);" + System.Environment.NewLine +
  279.                         "var removedId;" + System.Environment.NewLine +
  280.                         "if(objlist.length != null && objlist.length != 0)" +
  281.                         "{" +
  282.                             "for(i=objlist.length-1;i>=0;i--)" + System.Environment.NewLine +
  283.                             "{" + System.Environment.NewLine +
  284.                                 "if(objlist.options[i].selected == true)" + System.Environment.NewLine +
  285.                                 "{" + System.Environment.NewLine +
  286.                                     "removedId = objlist.options[i].value;" + System.Environment.NewLine +
  287.                                     "objlist.remove(i);" + System.Environment.NewLine +
  288.                                 "}" + System.Environment.NewLine +
  289.                             "}" + System.Environment.NewLine +
  290.                         "}" +
  291.                         "if(removedId == null || removedId == '')" + System.Environment.NewLine +
  292.                         "{" +
  293.                             "alert('没有选中成员');" + System.Environment.NewLine +
  294.                         "}" +
  295.                         "else" +
  296.                         "{" + System.Environment.NewLine +
  297.                     // 从ID文本中删除
  298.                             @"var selectedUsers = document.getElementById(selectedIdId).value;" + System.Environment.NewLine +
  299.                             @"var start = selectedUsers.indexOf(removedId);
  300.                             var preStr = selectedUsers.substring(0, start);
  301.                             var postStart = start + removedId.length + 1;
  302.                             var postEnd = selectedUsers.length;
  303.                             var postStr = selectedUsers.substring(postStart, postEnd);
  304.         
  305.                             document.getElementById(selectedIdId).value = preStr + postStr;
  306.                         }";
  307.                 writer.Write("<A onclick="javascript:" + clearScript + "" style="CURSOR: hand" >&lt;</A>");
  308.             }
  309.             writer.Write("</TD>");
  310.             writer.Write("<TD>");
  311.             this.SelectionText.Attributes.Add("style", "display:none;");
  312.             this.SelectionList.RenderControl(writer);
  313.             this.SelectionText.RenderControl(writer);
  314.             writer.Write("</TD>");
  315.             writer.Write("</TR>");
  316.             writer.Write("</TABLE>");
  317.         }
  318.     }
  319. }