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

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. [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
  10. namespace OThinker.H3.WorkSheet
  11. {
  12.     /// <summary>
  13.     /// UserSelector 的摘要说明。
  14.     /// </summary>
  15.     [ToolboxBitmap(typeof(UserSelector), "OThinker.H3.WorkSheet.UserSelector.bmp")]
  16.     [ToolboxData("<{0}:UserSelector runat=server></{0}:UserSelector>")]
  17.     public class UserSelector : System.Web.UI.WebControls.WebControl
  18.     {
  19.         /// <summary>
  20.         /// 选择模式
  21.         /// </summary>
  22.         public enum SelectionMode
  23.         {
  24.             /// <summary>
  25.             /// 选择单个
  26.             /// </summary>
  27.             Single,
  28.             /// <summary>
  29.             /// 选择多个
  30.             /// </summary>
  31.             Multi
  32.         }
  33.         private ListBox _UserList = new ListBox();
  34.         /// <summary>
  35.         /// 用于存储多值
  36.         /// </summary>
  37.         public ListBox UserList
  38.         {
  39.             get
  40.             {
  41.                 return this._UserList;
  42.             }
  43.         }
  44.         private TextBox _UserIDs = new TextBox();
  45.         /// <summary>
  46.         /// 用于存储用户ID
  47.         /// </summary>
  48.         public TextBox UserIDs
  49.         {
  50.             get
  51.             {
  52.                 return this._UserIDs;
  53.             }
  54.         }
  55.         private TextBox _User = new TextBox();
  56.         /// <summary>
  57.         /// 用于显示用户名
  58.         /// </summary>
  59.         public TextBox User
  60.         {
  61.             get
  62.             {
  63.                 return this._User;
  64.             }
  65.         }
  66.         public UserSelector()
  67.         {
  68.             // this.User.ReadOnly = true;
  69.             this.User.Attributes.Add("readonly", "readonly");
  70.             this.Controls.Add(this.User);
  71.             this.Controls.Add(this.UserList);
  72.             this.Controls.Add(this.UserIDs);
  73.         }
  74.         #region 选择模式
  75.         private SelectionMode _SelectionMode = SelectionMode.Single;
  76.         public virtual SelectionMode SelectMode
  77.         {
  78.             get
  79.             {
  80.                 return this._SelectionMode;
  81.             }
  82.             set
  83.             {
  84.                 this._SelectionMode = value;
  85.             }
  86.         }
  87.         #endregion
  88.         #region 选择用户的链接
  89.         private string _SelectUserUrl;
  90.         public virtual string SelectUserUrl
  91.         {
  92.             get
  93.             {
  94.                 return this._SelectUserUrl;
  95.             }
  96.             set
  97.             {
  98.                 this._SelectUserUrl = value;
  99.             }
  100.         }
  101.         private string _SelectUserImageUrl;
  102.         public virtual string SelectUserImageUrl
  103.         {
  104.             get
  105.             {
  106.                 return this._SelectUserImageUrl;
  107.             }
  108.             set
  109.             {
  110.                 this._SelectUserImageUrl = value;
  111.             }
  112.         }
  113.         private string _RemoveUserImageUrl;
  114.         public virtual string RemoveUserImageUrl
  115.         {
  116.             get
  117.             {
  118.                 return this._RemoveUserImageUrl;
  119.             }
  120.             set
  121.             {
  122.                 this._RemoveUserImageUrl = value;
  123.             }
  124.         }
  125.         #endregion
  126.         #region 允许选择的类型
  127.         private bool _CompanySelectable;
  128.         public bool CompanySelectable
  129.         {
  130.             get
  131.             {
  132.                 return this._CompanySelectable;
  133.             }
  134.             set
  135.             {
  136.                 this._CompanySelectable = value;
  137.             }
  138.         }
  139.         private bool _OrgUnitSelectable;
  140.         public bool OrgUnitSelectable
  141.         {
  142.             get
  143.             {
  144.                 return this._OrgUnitSelectable;
  145.             }
  146.             set
  147.             {
  148.                 this._OrgUnitSelectable = value;
  149.             }
  150.         }
  151.         private bool _GroupSelectable;
  152.         public bool GroupSelectable
  153.         {
  154.             get
  155.             {
  156.                 return this._GroupSelectable;
  157.             }
  158.             set
  159.             {
  160.                 this._GroupSelectable = value;
  161.             }
  162.         }
  163.         private bool _UserSelectable;
  164.         public bool UserSelectable
  165.         {
  166.             get
  167.             {
  168.                 return this._UserSelectable;
  169.             }
  170.             set
  171.             {
  172.                 this._UserSelectable = value;
  173.             }
  174.         }
  175.         #endregion
  176.         #region 可见的类型
  177.         private bool _CompanyVisible = true;
  178.         public bool CompanyVisible
  179.         {
  180.             get
  181.             {
  182.                 return this._CompanyVisible;
  183.             }
  184.             set
  185.             {
  186.                 this._CompanyVisible = value;
  187.             }
  188.         }
  189.         private bool _OrgUnitVisible = true;
  190.         public bool OrgUnitVisible
  191.         {
  192.             get
  193.             {
  194.                 return this._OrgUnitVisible;
  195.             }
  196.             set
  197.             {
  198.                 this._OrgUnitVisible = value;
  199.             }
  200.         }
  201.         private bool _GroupVisible = true;
  202.         public bool GroupVisible
  203.         {
  204.             get
  205.             {
  206.                 return this._GroupVisible;
  207.             }
  208.             set
  209.             {
  210.                 this._GroupVisible = value;
  211.             }
  212.         }
  213.         private bool _UserVisible = true;
  214.         public bool UserVisible
  215.         {
  216.             get
  217.             {
  218.                 return this._UserVisible;
  219.             }
  220.             set
  221.             {
  222.                 this._UserVisible = value;
  223.             }
  224.         }
  225.         #endregion
  226.         #region 可见类型
  227.         private OThinker.Organization.VisibleType _VisibleType = OThinker.Organization.VisibleType.Normal;
  228.         public OThinker.Organization.VisibleType VisibleType
  229.         {
  230.             get
  231.             {
  232.                 return this._VisibleType;
  233.             }
  234.             set
  235.             {
  236.                 this._VisibleType = value;
  237.             }
  238.         }
  239.         #endregion
  240.         #region 加载选中的值
  241.         public void LoadSelection(OThinker.Organization.IOrganization Organization, string[] SelectedUsers)
  242.         {
  243.             if (SelectedUsers != null)
  244.             {
  245.                 string text = null;
  246.                 foreach (string userId in SelectedUsers)
  247.                 {
  248.                     string name = Organization.GetName(userId);
  249.                     if (name == null)
  250.                     {
  251.                         continue;
  252.                     }
  253.                     System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(name, userId);
  254.                     this.UserList.Items.Add(item);
  255.                     // 初始化文本框
  256.                     text += item.Value + ";";
  257.                 }
  258.                 this.UserIDs.Text = text;
  259.             }
  260.         }
  261.         public void LoadSelection(OThinker.Organization.IOrganization Organization, string SelectedUser)
  262.         {
  263.             string name = Organization.GetName(SelectedUser);
  264.             if (name != null)
  265.             {
  266.                 this.User.Text = name;
  267.                 this.UserIDs.Text = SelectedUser;
  268.             }
  269.         }
  270.         #endregion
  271.         #region 选中的用户
  272.         [Browsable(false)]
  273.         public string[] SelectedUsers
  274.         {
  275.             get
  276.             {
  277.                 switch (this.SelectMode)
  278.                 {
  279.                     case UserSelector.SelectionMode.Single:
  280.                     case UserSelector.SelectionMode.Multi:
  281.                         string[] selectedUsers = new string[0];
  282.                         // 选中的用户的字符串
  283.                         string selectedUsersString = this.UserIDs.Text;
  284.                         if (selectedUsersString != null && selectedUsersString != "")
  285.                         {
  286.                             selectedUsers = selectedUsersString.Split(new char[] { ';' });
  287.                         }
  288.                         // 清除其中为空的数据
  289.                         ArrayList formattedUserList = new ArrayList();
  290.                         foreach (string item in selectedUsers)
  291.                         {
  292.                             if (item != null && item != "")
  293.                             {
  294.                                 formattedUserList.Add(item);
  295.                             }
  296.                         }
  297.                         return OThinker.Data.ArrayConvertor<string>.ToArray(formattedUserList);
  298.                     default:
  299.                         throw new NotImplementedException();
  300.                 }
  301.             }
  302.         }
  303.         [Browsable(false)]
  304.         public string SelectedUser
  305.         {
  306.             get
  307.             {
  308.                 string[] users = this.SelectedUsers;
  309.                 if (users == null || users.Length == 0)
  310.                 {
  311.                     return null;
  312.                 }
  313.                 else
  314.                 {
  315.                     return users[0];
  316.                 }
  317.             }
  318.         }
  319.         #endregion
  320.         #region 是否可编辑
  321.         private bool _Editable = true;
  322.         [Category("WorkSheet"), DefaultValue("")]
  323.         public virtual bool Editable
  324.         {
  325.             get
  326.             {
  327.                 return this._Editable;
  328.             }
  329.             set
  330.             {
  331.                 this._Editable = value;
  332.             }
  333.         }
  334.         #endregion
  335.         protected override void Render(HtmlTextWriter writer)
  336.         {
  337.             writer.Write("<TABLE WIDTH="" + this.Width + "" HEIGHT="" + this.Height + "" cellpadding="0" cellspacing="0" border="0">");
  338.             writer.Write("<TR VALIGN='TOP'>");
  339.             writer.Write("<TD>");// WIDTH="100%" HEIGHT="100%">");
  340.             this.UserIDs.Attributes.Add("style", "display:none;");
  341.             this.UserIDs.RenderControl(writer);
  342.             double usersWidth = this.Width.Value - 32;
  343.             if (usersWidth < 0)
  344.             {
  345.                 usersWidth = 0;
  346.             }
  347.             double usersHeight = this.Height.Value;
  348.             switch (this.SelectMode)
  349.             {
  350.                 case SelectionMode.Single:
  351.                     if (this.Width.Type == UnitType.Percentage)
  352.                     {
  353.                         this.User.Attributes.Add("style", "width:95%");
  354.                     }
  355.                     else
  356.                     {
  357.                         this.User.Width = new Unit(usersWidth);
  358.                     }
  359.                     this.User.RenderControl(writer);
  360.                     break;
  361.                 case SelectionMode.Multi:
  362.                     string styleAttrbites = null;
  363.                     if (this.Width.Type == UnitType.Percentage)
  364.                     {
  365.                         styleAttrbites = "width:100%;";
  366.                     }
  367.                     else
  368.                     {
  369.                         this.UserList.Width = new Unit(usersWidth);
  370.                     }
  371.                     if (this.Height.Type == UnitType.Percentage)
  372.                     {
  373.                         styleAttrbites += "height:100%;";
  374.                     }
  375.                     else
  376.                     {
  377.                         this.UserList.Height = new Unit(usersHeight);
  378.                     }
  379.                     if (styleAttrbites != null)
  380.                     {
  381.                         this.UserList.Attributes.Add("style", styleAttrbites);
  382.                     }
  383.                     this.UserList.RenderControl(writer);
  384.                     break;
  385.                 default:
  386.                     throw new NotImplementedException();
  387.             }
  388.             writer.Write("</TD>");
  389.             writer.Write("<TD>");
  390.             writer.Write("</TD>");
  391.             writer.Write("<TD>");
  392.             // 如果可编辑,则显示编辑按钮
  393.             if (this.Editable)
  394.             {
  395.                 int unitType =
  396.                     (this.CompanySelectable ? (int)OThinker.Organization.UnitType.Company : 0) |
  397.                     (this.OrgUnitSelectable ? (int)OThinker.Organization.UnitType.OrganizationUnit : 0) |
  398.                     (this.GroupSelectable ? (int)OThinker.Organization.UnitType.Group : 0) |
  399.                     (this.UserSelectable ? (int)OThinker.Organization.UnitType.User : 0);
  400.                 int visibleUnitType =
  401.                     (this.CompanyVisible ? (int)OThinker.Organization.UnitType.Company : 0) |
  402.                     (this.OrgUnitVisible ? (int)OThinker.Organization.UnitType.OrganizationUnit : 0) |
  403.                     (this.GroupVisible ? (int)OThinker.Organization.UnitType.Group : 0) |
  404.                     (this.UserVisible ? (int)OThinker.Organization.UnitType.User : 0);
  405.                 string url =
  406.                     this.SelectUserUrl + "?" +
  407.                     "UserControl=" + (this.SelectMode == UserSelector.SelectionMode.Single ? this.User.ClientID : this.UserList.ClientID) + "&" +
  408.                     "Mode=" + (this.SelectMode == UserSelector.SelectionMode.Single ? "Single" : "Multi") + "&" +
  409.                     "UserIDControl=" + this.UserIDs.ClientID + "&" +
  410.                     "UnitType=" + unitType.ToString() + "&" +
  411.                     "VisibleUnitType=" + visibleUnitType + "&" + 
  412.                     "VisibleType=" + (int)this.VisibleType;
  413.                 writer.Write("<IMG height="16" src="" + this.SelectUserImageUrl + "" onclick="javascript:childWindowProperties='toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=670,height=500,left=' + window.event.clientX +',top='+ window.event.clientY; window.open('" + url + "', '', childWindowProperties);" style="CURSOR: hand" >");
  414.                 string clearScript = null;
  415.                 if (this.SelectMode == SelectionMode.Single)
  416.                 {
  417.                     clearScript = "document.getElementById('" + this.User.ClientID + "').value='';document.getElementById('" + this.UserIDs.ClientID + "').value='';";
  418.                 }
  419.                 else
  420.                 {
  421.                     clearScript =
  422.                         "var listId='" + this.UserList.ClientID + "';" + System.Environment.NewLine +
  423.                         "var selectedIdId='" + this.UserIDs.ClientID + "';" + System.Environment.NewLine +
  424.                         "var objlist=document.getElementById(listId);" + System.Environment.NewLine +
  425.                         "var removedId;" + System.Environment.NewLine +
  426.                         "if(objlist.length != null && objlist.length != 0)" +
  427.                         "{" +
  428.                             "for(i=objlist.length-1;i>=0;i--)" + System.Environment.NewLine +
  429.                             "{" + System.Environment.NewLine +
  430.                                 "if(objlist.options[i].selected == true)" + System.Environment.NewLine +
  431.                                 "{" + System.Environment.NewLine +
  432.                                     "removedId = objlist.options[i].value;" + System.Environment.NewLine +
  433.                                     "objlist.remove(i);" + System.Environment.NewLine +
  434.                                 "}" + System.Environment.NewLine +
  435.                             "}" + System.Environment.NewLine +
  436.                         "}" +
  437.                         "if(removedId == null || removedId == '')" + System.Environment.NewLine +
  438.                         "{" +
  439.                             "alert('没有选中成员');" + System.Environment.NewLine +
  440.                         "}" +
  441.                         "else" +
  442.                         "{" + System.Environment.NewLine +
  443.                         // 从ID文本中删除
  444.                             @"var selectedUsers = document.getElementById(selectedIdId).value;" + System.Environment.NewLine +
  445.                             @"var start = selectedUsers.indexOf(removedId);
  446.                             var preStr = selectedUsers.substring(0, start);
  447.                             var postStart = start + removedId.length + 1;
  448.                             var postEnd = selectedUsers.length;
  449.                             var postStr = selectedUsers.substring(postStart, postEnd);
  450.         
  451.                             document.getElementById(selectedIdId).value = preStr + postStr;
  452.                         }";
  453.                 }
  454.                 writer.Write("<IMG height="16" src="" + this.RemoveUserImageUrl + "" onclick="javascript:" + clearScript + "" style="CURSOR: hand" >");
  455.             }
  456.             writer.Write("</TD>");
  457.             writer.Write("</TR>");
  458.             writer.Write("</TABLE>");
  459.         }
  460.     }
  461. }