using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Web.UI.WebControls; using System.ComponentModel; using System.Data.SqlClient; using System.Text; using DBUtility; #region histroy ///程式代號:UCDropDownList ///程式名稱:系統共用下拉選單 ///程式說明: ///xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容) ///01.2012/08/10 1.0 Ethan CREATE ///02.2014/03/05 1.1 Dean _FullText 改為如果有|| 符號則前面的為文字後面的為值 #endregion namespace OT.Controls { public class DropDownList : System.Web.UI.WebControls.DropDownList { #region Declare Region private Boolean _IsSubTextLength = false; //是否截取Text長度 private int _TextLength = 30; //文字的最大長度 private BindType _IsShowFullText = BindType.No; //是否設置'全部' private string _FullText = "請選擇..."; //未選擇時的的預設第一個值設定 private Boolean _IsValueOrText = true; //是否相反顯示Text或Value值(True:text-value,False:Value-text) private Boolean _IsHtmlDecode = false; //是否將顯示的Text值HtmlDecode加碼 private DropDownListType _DrpType = DropDownListType.None; //下拉框類別 private TextType _IsShowID = TextType.No; //是否顯示ID private string _selectedtext = string.Empty; //設定下拉框選中的Text值 private string parameterValue; //SQL參數 private static readonly object EventUCInit = new object(); #endregion #region Property Region #region IsSubTextLength /// /// 是否截取文字顯示的最大長度. 預設30 /// [Category("Behavior"), DefaultValue(false), Description("是否截取文字顯示的最大長度. 預設30")] public Boolean IsSubTextLength { get { return _IsSubTextLength; } set { _IsSubTextLength = value; } } #endregion #region TextLength /// /// 文字顯示的最大長度 /// [Category("Behavior"), DefaultValue(30), Description("文字顯示的最大長度")] public int TextLength { get { return _TextLength; } set { _TextLength = value; } } #endregion #region IsShowFullText /// /// 是否顯示'全部'選項 /// [Category("Behavior"), DefaultValue(BindType.No), Description("是否顯示'全部'選項")] public BindType IsShowFullText { get { return _IsShowFullText; } set { _IsShowFullText = value; } } #endregion #region FullText /// /// 『全部』的客制化設定 /// [Category("Behavior"), DefaultValue("請選擇"), Description("『全部』的客制化設定")] public string FullText { get { return _FullText; } set { _FullText = value; } } #endregion #region IsValueOrText /// /// 是否相反顯示Text或Value /// [Category("Behavior"), DefaultValue(true), Description("text 顯示格式,需要和IsShowID配合使用 true:value - text;false:text - value")] public Boolean IsValueOrText { get { return _IsValueOrText; } set { _IsValueOrText = value; } } #endregion #region IsHtmlDecode /// /// 是否將顯示的Text值HtmlDecode加碼 /// [Category("Behavior"), DefaultValue(false), Description("是否將顯示的Text值HtmlDecode加碼")] public Boolean IsHtmlDecode { get { return _IsHtmlDecode; } set { _IsHtmlDecode = value; } } #endregion #region DrpType /// /// 下拉框類別 /// [Category("Behavior"), DefaultValue(DropDownListType.None), Description("下拉框類別")] public DropDownListType DrpType { get { return _DrpType; } set { _DrpType = value; if (_DrpType != DropDownListType.None) { BindDrp(); } } } #endregion #region IsShowID /// /// 是否設置ID /// [Category("Behavior"), DefaultValue(TextType.No), Description("是否設置顯示'ID'")] public TextType IsShowID { get { return _IsShowID; } set { _IsShowID = value; } } #endregion #region SelectedText /// /// 設定下拉框選中的Text值 /// [Category("Behavior"), DefaultValue(""), Description("讀取或設定下拉框選中的Text值")] public string SelectedText { get { return base.SelectedItem.Text; } set { bool b_Flag = true; foreach (System.Web.UI.WebControls.ListItem li in this.Items) { li.Selected = false; if (li.Text.Equals(value)) { li.Selected = true; b_Flag = false; } } if (b_Flag) this.SelectedIndex = 0; } } #endregion #region SelectedValue /// /// 設定下拉框選中的Value值" /// public override string SelectedValue { get { return base.SelectedValue; } set { if (base.Items.FindByValue(value) != null) { base.SelectedValue = value; } else { base.SelectedIndex = 0; } } } #endregion #region Value /// /// 設定下拉框選中的Value值 /// [Category("Behavior"), DefaultValue(""), Description("讀取或設定下拉框選中的Value值")] public string Value { get { return this.SelectedValue; } set { this.SelectedValue = value; } } #endregion public override Unit Width { set { base.Width = value; } get { return base.Width; } } public string ParameterValue { get { return parameterValue; } set { parameterValue = value; } } public event EventHandler UCInit { add { base.Events.AddHandler(EventUCInit, value); } remove { base.Events.RemoveHandler(EventUCInit, value); } } #endregion #region Definition Private Methods protected virtual void OnUCInit(EventArgs e) { EventHandler handler = (EventHandler)Events[EventUCInit]; if (handler != null) { handler(this, e); } } /// /// 複寫OnInit方法 /// /// protected override void OnInit(EventArgs e) { try { base.OnInit(e); OnUCInit(EventArgs.Empty); if (_DrpType != DropDownListType.None) BindDrp(); } catch (Exception ex) { throw new Exception(ex.ToString()); } } /// /// 綁定下拉框 /// public void BindDrp() { this.Items.Clear(); if (this.Page != null) { if (this.Page.Site != null && this.Page.Site.DesignMode) { return; } } //獲取SQL語句 string strSQL = getSQL(); if (strSQL == "") { strSQL = "SELECT '' [A], '' [B]"; } DataTable o_dt = DbHelperSQL.Query(strSQL).Tables[0]; if (o_dt != null && o_dt.Rows.Count > 0) { foreach (DataRow row in o_dt.Rows) { string strValue = row[0].ToString(); string strText = row[1].ToString(); string strFullText = string.Empty; if (IsShowID == TextType.Yes) { if (IsValueOrText) { strText = strValue + " - " + strText; } else { strText = strText + " - " + strValue; } } if (IsHtmlDecode) { //如果需要HtmlDecode編碼 strText = HttpUtility.HtmlDecode(strText); } strFullText = strText; if (IsSubTextLength && strText.Length > TextLength) { if (TextLength > 3) { strText = strText.Substring(0, TextLength - 3) + "..."; } else { strText = strText.Substring(0, TextLength) + "..."; } } ListItem item = new ListItem(strText, strValue); item.Attributes.Add("title", strFullText); this.Items.Add(item); } } ///2014/03/05 1.1 Dean _FullText 改為如果有|| 符號則前面的為文字後面的為值 if (IsShowFullText == BindType.Yes) { if (FullText.IndexOf("||") > 0) { this.Items.Insert(0, new System.Web.UI.WebControls.ListItem(FullText.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries).GetValue(0).ToString(), FullText.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString())); } else { this.Items.Insert(0, new System.Web.UI.WebControls.ListItem(FullText, "")); } } } /// /// 查詢SQL語句 /// /// strSQL private string getSQL() { string strSQL = ""; switch (DrpType) { case DropDownListType.None://暫時無效列表 Add by Ethan, 2012/08/17. strSQL = " SELECT ArgumentClassID,ArgumentClassName FROM OTB_SYS_Argumentclass WHERE Effective='1' "; break; case DropDownListType.ArgumentClassList://參數類別有效列表,主要用於新增時全選的地方 Add by Ethan, 2012/08/17. strSQL = " SELECT ArgumentClassID,ArgumentClassName FROM OTB_SYS_Argumentclass WHERE Effective='Y' AND DelStatus='N' ORDER BY OrderByValue "; break; case DropDownListType.ArgumentClassListAll://參數類別全部列表 Add by Alina, 2012/11/21. strSQL = " SELECT ArgumentClassID,ArgumentClassName FROM OTB_SYS_Argumentclass WHERE Effective='Y' ORDER BY OrderByValue "; break; case DropDownListType.RoleList://角色信息有效列表 Add by Ethan, 2012/08/20. strSQL = " SELECT RuleID,RuleName FROM OTB_SYS_Rules WHERE DelStatus='N' ORDER BY RuleID "; break; case DropDownListType.ModuleList://模組維護信息列表 Add by Alina,模組選擇上層代碼時 2012/08/20. strSQL = " SELECT ModuleID,ModuleName FROM OTB_SYS_ModuleList ORDER BY OrderByValue"; break; case DropDownListType.Department://部門有效列表 Add by Alina, 職稱中會選擇部門 2013/11/20. strSQL = " SELECT DepartmentID,DepartmentName FROM OTB_SYS_Departments WHERE Effective='Y' ORDER BY DepartmentID "; break; case DropDownListType.ChiefID://主管代號 Add by Jack, 2014/07/21. strSQL = "SELECT MemberID,MemberName FROM dbo.OTB_SYS_Members AS b LEFT JOIN dbo.OTB_SYS_Jobtitle AS j ON b.JobTitleID=j.JobtitleID WHERE j.JobtitleID='001'"; break; case DropDownListType.DeptIDList: //部門信息列表 Add by John, 職稱部分需要當查詢條件2012/11/26. strSQL = " SELECT DepartmentID,DepartmentName FROM OTB_SYS_Departments WHERE Effective='Y' ORDER BY DepartmentName"; break; case DropDownListType.JobtitleList://職稱信息列表 Add by John,人員處需要該部分 2013/11/26. strSQL = " SELECT JobtitleID,JobtitleName AS JobtitleName FROM OTB_SYS_Jobtitle WHERE Effective='Y' ORDER BY JobtitleName"; break; case DropDownListType.MemberList://人員信息列表 Add by John,人員處需要該部分 2013/11/26. strSQL = " SELECT MemberID ,MemberName FROM dbo.OTB_SYS_Members "; break; case DropDownListType.JobClass://工作類別列表 Add by Alina,人員處需要該部分 2015/7/29. strSQL = " SELECT ArgumentID,ArgumentValue FROM dbo.OTB_SYS_Arguments WHERE Effective='Y' AND DelStatus='N' AND ArgumentClassID='JobClass' "; break; case DropDownListType.TableDirectory://資料表列表 Add by Gary 20141013 strSQL = " select distinct form_id as Table_NameC,form_id as Table_NameE from OTB_SYS_TableDirectory order by form_id "; break; } return strSQL; } #endregion #region Definition Public Methods #endregion } #region 枚舉類型 /// /// 是否顯示未選擇 /// public enum BindType { /// /// 否 /// No, /// /// 是 /// Yes } /// /// 下拉框類別枚舉 /// public enum DropDownListType { #region 枚舉類型 /// /// 0、預設 /// None = 0, /// /// 參數類別有效列表 /// ArgumentClassList, /// /// 參數類別全部列表 /// ArgumentClassListAll, /// /// 角色信息有效列表 /// RoleList, /// /// 模組維護信息列表 /// ModuleList, /// ///部門列表 /// Department, /// /// 部門信息列表 /// DeptIDList, /// /// 職稱信息列表 /// JobtitleList, /// ///主管代號 //Add By Jack 2014/07/21 /// ChiefID, /// ///人員信息列表 /// MemberList, /// /// 工作類別 /// JobClass, /// /// 資料表列表 /// TableDirectory, #endregion } /// /// 是否顯示代號,當查詢到BSYSDATA里的數據時用此屬性 /// public enum TextType { /// /// 是顯示 /// Yes, /// /// 否不顯示 /// No } #endregion }