UserSelector.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:17k
源码类别:
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;
- [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
- namespace OThinker.H3.WorkSheet
- {
- /// <summary>
- /// UserSelector 的摘要说明。
- /// </summary>
- [ToolboxBitmap(typeof(UserSelector), "OThinker.H3.WorkSheet.UserSelector.bmp")]
- [ToolboxData("<{0}:UserSelector runat=server></{0}:UserSelector>")]
- public class UserSelector : System.Web.UI.WebControls.WebControl
- {
- /// <summary>
- /// 选择模式
- /// </summary>
- public enum SelectionMode
- {
- /// <summary>
- /// 选择单个
- /// </summary>
- Single,
- /// <summary>
- /// 选择多个
- /// </summary>
- Multi
- }
- private ListBox _UserList = new ListBox();
- /// <summary>
- /// 用于存储多值
- /// </summary>
- public ListBox UserList
- {
- get
- {
- return this._UserList;
- }
- }
- private TextBox _UserIDs = new TextBox();
- /// <summary>
- /// 用于存储用户ID
- /// </summary>
- public TextBox UserIDs
- {
- get
- {
- return this._UserIDs;
- }
- }
- private TextBox _User = new TextBox();
- /// <summary>
- /// 用于显示用户名
- /// </summary>
- public TextBox User
- {
- get
- {
- return this._User;
- }
- }
- public UserSelector()
- {
- // this.User.ReadOnly = true;
- this.User.Attributes.Add("readonly", "readonly");
- this.Controls.Add(this.User);
- this.Controls.Add(this.UserList);
- this.Controls.Add(this.UserIDs);
- }
- #region 选择模式
- private SelectionMode _SelectionMode = SelectionMode.Single;
- public virtual SelectionMode SelectMode
- {
- get
- {
- return this._SelectionMode;
- }
- set
- {
- this._SelectionMode = value;
- }
- }
- #endregion
- #region 选择用户的链接
- private string _SelectUserUrl;
- public virtual string SelectUserUrl
- {
- get
- {
- return this._SelectUserUrl;
- }
- set
- {
- this._SelectUserUrl = value;
- }
- }
- private string _SelectUserImageUrl;
- public virtual string SelectUserImageUrl
- {
- get
- {
- return this._SelectUserImageUrl;
- }
- set
- {
- this._SelectUserImageUrl = value;
- }
- }
- private string _RemoveUserImageUrl;
- public virtual string RemoveUserImageUrl
- {
- get
- {
- return this._RemoveUserImageUrl;
- }
- set
- {
- this._RemoveUserImageUrl = value;
- }
- }
- #endregion
- #region 允许选择的类型
- private bool _CompanySelectable;
- public bool CompanySelectable
- {
- get
- {
- return this._CompanySelectable;
- }
- set
- {
- this._CompanySelectable = value;
- }
- }
- private bool _OrgUnitSelectable;
- public bool OrgUnitSelectable
- {
- get
- {
- return this._OrgUnitSelectable;
- }
- set
- {
- this._OrgUnitSelectable = value;
- }
- }
- private bool _GroupSelectable;
- public bool GroupSelectable
- {
- get
- {
- return this._GroupSelectable;
- }
- set
- {
- this._GroupSelectable = value;
- }
- }
- private bool _UserSelectable;
- public bool UserSelectable
- {
- get
- {
- return this._UserSelectable;
- }
- set
- {
- this._UserSelectable = value;
- }
- }
- #endregion
- #region 可见的类型
- private bool _CompanyVisible = true;
- public bool CompanyVisible
- {
- get
- {
- return this._CompanyVisible;
- }
- set
- {
- this._CompanyVisible = value;
- }
- }
- private bool _OrgUnitVisible = true;
- public bool OrgUnitVisible
- {
- get
- {
- return this._OrgUnitVisible;
- }
- set
- {
- this._OrgUnitVisible = value;
- }
- }
- private bool _GroupVisible = true;
- public bool GroupVisible
- {
- get
- {
- return this._GroupVisible;
- }
- set
- {
- this._GroupVisible = value;
- }
- }
- private bool _UserVisible = true;
- public bool UserVisible
- {
- get
- {
- return this._UserVisible;
- }
- set
- {
- this._UserVisible = value;
- }
- }
- #endregion
- #region 可见类型
- private OThinker.Organization.VisibleType _VisibleType = OThinker.Organization.VisibleType.Normal;
- public OThinker.Organization.VisibleType VisibleType
- {
- get
- {
- return this._VisibleType;
- }
- set
- {
- this._VisibleType = value;
- }
- }
- #endregion
- #region 加载选中的值
- public void LoadSelection(OThinker.Organization.IOrganization Organization, string[] SelectedUsers)
- {
- if (SelectedUsers != null)
- {
- string text = null;
- foreach (string userId in SelectedUsers)
- {
- string name = Organization.GetName(userId);
- if (name == null)
- {
- continue;
- }
- System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(name, userId);
- this.UserList.Items.Add(item);
- // 初始化文本框
- text += item.Value + ";";
- }
- this.UserIDs.Text = text;
- }
- }
- public void LoadSelection(OThinker.Organization.IOrganization Organization, string SelectedUser)
- {
- string name = Organization.GetName(SelectedUser);
- if (name != null)
- {
- this.User.Text = name;
- this.UserIDs.Text = SelectedUser;
- }
- }
- #endregion
- #region 选中的用户
- [Browsable(false)]
- public string[] SelectedUsers
- {
- get
- {
- switch (this.SelectMode)
- {
- case UserSelector.SelectionMode.Single:
- case UserSelector.SelectionMode.Multi:
- string[] selectedUsers = new string[0];
- // 选中的用户的字符串
- string selectedUsersString = this.UserIDs.Text;
- if (selectedUsersString != null && selectedUsersString != "")
- {
- selectedUsers = selectedUsersString.Split(new char[] { ';' });
- }
- // 清除其中为空的数据
- ArrayList formattedUserList = new ArrayList();
- foreach (string item in selectedUsers)
- {
- if (item != null && item != "")
- {
- formattedUserList.Add(item);
- }
- }
- return OThinker.Data.ArrayConvertor<string>.ToArray(formattedUserList);
- default:
- throw new NotImplementedException();
- }
- }
- }
- [Browsable(false)]
- public string SelectedUser
- {
- get
- {
- string[] users = this.SelectedUsers;
- if (users == null || users.Length == 0)
- {
- return null;
- }
- else
- {
- return users[0];
- }
- }
- }
- #endregion
- #region 是否可编辑
- private bool _Editable = true;
- [Category("WorkSheet"), DefaultValue("")]
- public virtual bool Editable
- {
- get
- {
- return this._Editable;
- }
- set
- {
- this._Editable = value;
- }
- }
- #endregion
- protected override void Render(HtmlTextWriter writer)
- {
- writer.Write("<TABLE WIDTH="" + this.Width + "" HEIGHT="" + this.Height + "" cellpadding="0" cellspacing="0" border="0">");
- writer.Write("<TR VALIGN='TOP'>");
- writer.Write("<TD>");// WIDTH="100%" HEIGHT="100%">");
- this.UserIDs.Attributes.Add("style", "display:none;");
- this.UserIDs.RenderControl(writer);
- double usersWidth = this.Width.Value - 32;
- if (usersWidth < 0)
- {
- usersWidth = 0;
- }
- double usersHeight = this.Height.Value;
- switch (this.SelectMode)
- {
- case SelectionMode.Single:
- if (this.Width.Type == UnitType.Percentage)
- {
- this.User.Attributes.Add("style", "width:95%");
- }
- else
- {
- this.User.Width = new Unit(usersWidth);
- }
- this.User.RenderControl(writer);
- break;
- case SelectionMode.Multi:
- string styleAttrbites = null;
- if (this.Width.Type == UnitType.Percentage)
- {
- styleAttrbites = "width:100%;";
- }
- else
- {
- this.UserList.Width = new Unit(usersWidth);
- }
- if (this.Height.Type == UnitType.Percentage)
- {
- styleAttrbites += "height:100%;";
- }
- else
- {
- this.UserList.Height = new Unit(usersHeight);
- }
- if (styleAttrbites != null)
- {
- this.UserList.Attributes.Add("style", styleAttrbites);
- }
- this.UserList.RenderControl(writer);
- break;
- default:
- throw new NotImplementedException();
- }
- writer.Write("</TD>");
- writer.Write("<TD>");
- writer.Write("</TD>");
- writer.Write("<TD>");
- // 如果可编辑,则显示编辑按钮
- if (this.Editable)
- {
- int unitType =
- (this.CompanySelectable ? (int)OThinker.Organization.UnitType.Company : 0) |
- (this.OrgUnitSelectable ? (int)OThinker.Organization.UnitType.OrganizationUnit : 0) |
- (this.GroupSelectable ? (int)OThinker.Organization.UnitType.Group : 0) |
- (this.UserSelectable ? (int)OThinker.Organization.UnitType.User : 0);
- int visibleUnitType =
- (this.CompanyVisible ? (int)OThinker.Organization.UnitType.Company : 0) |
- (this.OrgUnitVisible ? (int)OThinker.Organization.UnitType.OrganizationUnit : 0) |
- (this.GroupVisible ? (int)OThinker.Organization.UnitType.Group : 0) |
- (this.UserVisible ? (int)OThinker.Organization.UnitType.User : 0);
- string url =
- this.SelectUserUrl + "?" +
- "UserControl=" + (this.SelectMode == UserSelector.SelectionMode.Single ? this.User.ClientID : this.UserList.ClientID) + "&" +
- "Mode=" + (this.SelectMode == UserSelector.SelectionMode.Single ? "Single" : "Multi") + "&" +
- "UserIDControl=" + this.UserIDs.ClientID + "&" +
- "UnitType=" + unitType.ToString() + "&" +
- "VisibleUnitType=" + visibleUnitType + "&" +
- "VisibleType=" + (int)this.VisibleType;
- 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" >");
- string clearScript = null;
- if (this.SelectMode == SelectionMode.Single)
- {
- clearScript = "document.getElementById('" + this.User.ClientID + "').value='';document.getElementById('" + this.UserIDs.ClientID + "').value='';";
- }
- else
- {
- clearScript =
- "var listId='" + this.UserList.ClientID + "';" + System.Environment.NewLine +
- "var selectedIdId='" + this.UserIDs.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("<IMG height="16" src="" + this.RemoveUserImageUrl + "" onclick="javascript:" + clearScript + "" style="CURSOR: hand" >");
- }
- writer.Write("</TD>");
- writer.Write("</TR>");
- writer.Write("</TABLE>");
- }
- }
- }