DOGNET

  博客园 :: 首页 :: 联系 :: 订阅 订阅 :: 管理
  10 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

公告

2010年5月18日 #

.net 3.5+VS 2008

 

页面:AutoCompleted.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleted.ascx.cs"
    Inherits="ExpenseManager.Common.Controls.AutoCompleted" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<div id="listPlacement" runat="server" style="height: 150px; overflow: auto; display: none;">
</div>
<input id="txtValue" type="text" runat="server" style="display: none;" />
<div style="display: none;">
<cc1:AutoCompleteExtender ID="txtInput_AutoCompleteExtender" runat="server" DelimiterCharacters=""
    Enabled="True" ServicePath="AjaxHelper.asmx" CompletionSetCount="20" MinimumPrefixLength="2"
    CompletionListElementID="listPlacement" CompletionInterval="200" ServiceMethod="Get"
    TargetControlID="txtInput">
</cc1:AutoCompleteExtender>
</div>

 

代码: AutoCompleted.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace ExpenseManager.Common.Controls
{
    public partial class AutoCompleted : System.Web.UI.UserControl, IPostBackEventHandler
    {
        protected static object SELECTED = new object();

        public event EventHandler<AotoCompletedEventArgs> Selected
        {
            add
            {
                this.Events.AddHandler(SELECTED, value);
            }
            remove
            {
                this.Events.RemoveHandler(SELECTED, value);
            }
        }

        protected void OnSelected(AotoCompletedEventArgs e)
        {
            EventHandler<AotoCompletedEventArgs> temp = this.Events[SELECTED] as EventHandler<AotoCompletedEventArgs>;
            if(temp != null)
                temp(this, e);
        }

        public string ServicePath
        {
            get
            {
                return this.txtInput_AutoCompleteExtender.ServicePath;
            }
            set
            {
                this.txtInput_AutoCompleteExtender.ServicePath = value;
            }
        }

        public string ServiceMethod
        {
            get
            {
                return this.txtInput_AutoCompleteExtender.ServiceMethod;
            }
            set
            {
                this.txtInput_AutoCompleteExtender.ServiceMethod = value;
            }
        }

        public int CompletionInterval
        {
            get
            {
                return this.txtInput_AutoCompleteExtender.CompletionInterval;
            }
            set
            {
                this.txtInput_AutoCompleteExtender.CompletionInterval = value;
            }
        }

        public int CompletionSetCount
        {
            get
            {
                return this.txtInput_AutoCompleteExtender.CompletionSetCount;
            }
            set
            {
                this.txtInput_AutoCompleteExtender.CompletionSetCount = value;
            }
        }

        public virtual void RaisePostBackEvent(string eventArgument)
        {
            AotoCompletedEventArgs e = new AotoCompletedEventArgs();
            e.Data = new KeyValuePair<string, string>(this.txtInput.Text, this.txtValue.Value);
            this.OnSelected(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            EventHandler<AotoCompletedEventArgs> temp = this.Events[SELECTED] as EventHandler<AotoCompletedEventArgs>;
            string clientFunction = string.Format("{0}OnItemSelected", this.ClientID);
            if(temp != null)
            {
                this.txtInput_AutoCompleteExtender.OnClientItemSelected = clientFunction;
                string script = string.Format(@"function {0}(source, eventArgs)
                        {{
                                document.getElementById('{1}').value=eventArgs.get_value();
                                __doPostBack('{2}','SELECTED');
                        }}", clientFunction, txtValue.ClientID, this.ClientID);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), clientFunction, script, true);
            }
        }

        /// <summary>
        /// 合作单位名称绑定
        /// </summary>
        public string CropUnitName
        {
            set
            {
                this.txtInput.Text = value;
            }
        }

        /// <summary>
        /// 合作单位能否编辑
        /// </summary>
        public bool CropUnitEnabled
        {
            set
            {
                this.txtInput.Enabled = value;
            }
        }

        /// <summary>
        /// 控件宽度
        /// </summary>
        public Unit Width
        {
            get
            {
                return this.txtInput.Width;
            }
            set
            {
                this.txtInput.Width = value;
            }
        }
    }

    public class AotoCompletedEventArgs : EventArgs
    {
        public KeyValuePair<string, string> Data
        {
            get;
            set;
        }
    }
}

 

为控件实例添加选中项事件:

        protected void Page_Load(object sender, EventArgs e)
        {
            this.CCDEPaymentUnit.Selected += new EventHandler<ExpenseManager.Common.Controls.AotoCompletedEventArgs>                      (CCPaymentUnit_Selected);

        }

 

        void CCPaymentUnit_Selected(object sender, ExpenseManager.Common.Controls.AotoCompletedEventArgs e)
        {

                //得到选中项的值
                string strValue = e.Data.Value;
        }

 

posted @ 2010-05-18 16:29 DOGNET 阅读(148) 评论(0) 编辑

 .net 3.5环境

页面:ExpenseType.ascx

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ExpenseType.ascx.cs" Inherits="EM.ExpenseManager.Common.Controls.ExpenseType" %>
<asp:DropDownList ID="ddlLevel1" runat="server" DataTextField="CodeName" DataValueField="CodeNum" AutoPostBack="True"
    onselectedindexchanged="ddlLevel1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlLevel2" runat="server" DataTextField="CodeName" DataValueField="CodeNum">
</asp:DropDownList>

 

代码:ExpenseType.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using UIS = EM.ExpenseManager.Common.BaseInfoAS;
using UIS = EM.ExpenseManager.Common.UIService;


namespace EM.ExpenseManager.Common.Controls
{
    public partial class ExpenseType : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindLevel1();
                Reset();
                Reset(this.Level1SelectedIndex, this.Level2SelectedIndex);
            }
        }

        /// <summary>
        /// 绑定第一级
        /// </summary>
        public void BindLevel1()
        {
            Dictionary<string, string> list = UIS.GetExpenseTypeLevel1Cached();
            //Dictionary<string, string> list = UIS.GetExpenseTypeLevel1();

            this.ddlLevel1.Items.Clear();

            this.ddlLevel1.DataTextField = "value";
            this.ddlLevel1.DataValueField = "key";
            this.ddlLevel1.DataSource = list;
            this.ddlLevel1.DataBind();
            this.ddlLevel1.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        /// <summary>
        /// 根据第一级选中项绑定第二级
        /// </summary>
        /// <param name="level1CodeNum"></param>
        private void BindLevel2(string level1CodeNum)
        {
            this.ddlLevel2.Items.Clear();
            this.ddlLevel2.Enabled = false;

            if (level1CodeNum != "-1")
            {
                Dictionary<string, string> list = UIS.GetExpenseTypeLevel2Cached(level1CodeNum);
                //Dictionary<string, string> list = UIS.GetExpenseTypeLevel2(level1CodeNum);

                if (list.Count > 0)
                {
                    this.ddlLevel2.DataTextField = "value";
                    this.ddlLevel2.DataValueField = "key";

                    this.ddlLevel2.DataSource = list;
                    this.ddlLevel2.DataBind();

                    //this.ddlLevel2.Items.Insert(0, new ListItem("请选择", "-1"));
                    this.ddlLevel2.Enabled = true;
                }
                else
                {
                    this.ddlLevel2.Enabled = false;
                }
            }
        }

        /// <summary>
        /// 设置成初始状态
        /// </summary>
        public void Reset()
        {
            this.ddlLevel1.SelectedIndex = 0;
            this.ddlLevel2.Items.Clear();

            this.ddlLevel2.Enabled = false;
            BindLevel2(this.Level1SelectedValue);
        }

        /// <summary>
        /// 设置成初始状态(指定二级索引)
        /// </summary>
        /// <param name="iIndex1"></param>
        /// <param name="iIndex2"></param>
        public void Reset(int iIndex1,int iIndex2)
        {
            this.ddlLevel1.SelectedIndex = iIndex1;
            this.ddlLevel2.Items.Clear();

            this.ddlLevel2.Enabled = false;
            BindLevel2(this.Level1SelectedValue);
            this.ddlLevel2.SelectedIndex = iIndex2;

            //正常情况下ET控件载入时第一级Enabled为true,第二级为false;
            if (this._ETEnabled != true)
            {
                this.ddlLevel1.Enabled = this._ETEnabled;
                this.ddlLevel2.Enabled = this._ETEnabled;
            }
        }
        /// <summary>
        /// 第一级选中项的Value
        /// </summary>
        public string Level1SelectedValue
        {
            get { return this.ddlLevel1.SelectedValue; }
        }

        /// <summary>
        /// 第二级选中项的Value
        /// </summary>
        public string Level2SelectedValue
        {
            get { return this.ddlLevel2.SelectedValue; }
        }
        ///// <summary>
        ///// 第二级选中项的Value(若当前级不可选则返回空串)
        ///// </summary>
        //public string Level2SelectedValue
        //{
        //    get
        //    {
        //        if (this.ddlLevel2.Enabled == false)
        //        {
        //            return string.Empty;
        //        }
        //        else
        //        {
        //            return this.ddlLevel2.SelectedValue;
        //        }
        //    }
        //}

        ///// <summary>
        ///// 第二级选中项的Text
        ///// </summary>
        //public string Level2SelectedText
        //{
        //    get
        //    {
        //        if (this.ddlLevel2.Enabled == true)
        //        {
        //            return this.ddlLevel2.SelectedItem.Text;
        //        }
        //        else
        //        {
        //            return string.Empty;
        //        }
        //    }
        //}
        /// <summary>
        /// 第二级是否可选
        /// </summary>
        public bool Level2Enabled
        {
            get { return this.ddlLevel2.Enabled; }
        }

        /// <summary>
        /// 第-级选中项改变时重新绑定第二级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlLevel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindLevel2(Level1SelectedValue);
        }

        /// <summary>
        /// 得到控件选中项的Value
        /// </summary>
        public string SelectedValue
        {
            get
            {
                if (this.Level2Enabled==true)
                {
                    return this.ddlLevel2.SelectedValue;
                }
                else
                {
                    //若第一级也不可用则说明是通过ETEnabled=false故意将当前控件置为不可用
                    //因为默认情况下第一级是可用的
                    if (this.ddlLevel1.Enabled==false)
                    {
                        return this.ddlLevel2.SelectedValue;
                    }
                    else
                    {
                        return "-1";
                    }
                }
            }
        }


        /// <summary>
        /// 得到控件选中项的Text
        /// </summary>
        public string SelectedText
        {
            get
            {
                if (this.Level2Enabled == true)
                {
                    return this.ddlLevel2.SelectedItem.Text;
                }
                else
                {
                    if (this.ddlLevel1.Enabled == false)
                    {
                        return this.ddlLevel2.SelectedItem.Text;
                    }
                    else
                    {
                        return "-1";
                    }
                }
            }

        }

        /// <summary>
        /// 得到控件第一级选中项的Text
        /// </summary>
        public string Level1SelectedText
        {
            get
            {
                return this.ddlLevel1.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 得到控件第二级选中项的Text
        /// </summary>
        public string Level2SelectedText
        {
            get
            {
                return this.ddlLevel2.SelectedItem.Text;
            }
        }

        /// <summary>
        /// 重新绑定第一级选定项
        /// </summary>
        public void RefreshItemBinding()
        {
            BindLevel1();
            Reset();
        }

        /// <summary>
        /// 选定费用类型第一级和第二级中的指定项
        /// </summary>
        /// <param name="strLevel1Value"></param>
        /// <param name="strLevel2Value"></param>
        public void SetSelectedItem(ExpenseType et, string strLevel1Value, string strLevel2Value)
        {
            et.RefreshItemBinding();

            et.Level1SelectedIndex = et.ddlLevel1.Items.IndexOf(et.ddlLevel1.Items.FindByValue(strLevel1Value));
            et.BindLevel2(strLevel1Value);
            et.Level2SelectedIndex = et.ddlLevel2.Items.IndexOf(et.ddlLevel2.Items.FindByValue(strLevel2Value));
        }

        private bool _ETEnabled = true;
        //private bool _ETEnabled;
        /// <summary>
        /// 控件是否可用
        /// </summary>
        public bool ETEnabled
        {
            get { return this._ETEnabled; }
            set { this._ETEnabled = value; }

        }

        private int _Level1SelectedIndex = 0;
        /// <summary>
        /// 得到或设置第一级的选中索引
        /// </summary>
        public int Level1SelectedIndex
        {
            get { return this._Level1SelectedIndex; }
            set { this._Level1SelectedIndex = value; }
        }

        private int _Level2SelectedIndex = 0;
        /// <summary>
        /// 得到或设置第二级的选中索引
        /// </summary>
        public int Level2SelectedIndex
        {
            get { return this._Level2SelectedIndex; }
            set { this._Level2SelectedIndex = value; }
        }

    }

 

 

posted @ 2010-05-18 16:11 DOGNET 阅读(132) 评论(0) 编辑

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DogNet.Common
{
    /// <summary>
    /// 枚举类型的服务类
    /// </summary>
    public class EnumTypeService
    {
        /// <summary>
        /// 从枚举类型和它的特性读出并返回一个键值对(枚举值和中文描述)
        /// </summary>
        /// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        /// <returns>键值对(枚举值和描述)</returns>
        public static Dictionary<string, string> GetNVCFromEnumValue(Type enumType)
        {
            try
            {
                Dictionary<string, string> nvc = new Dictionary<string, string>();

                Type typeDescription = typeof(DescriptionAttribute);
                FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
                        strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                        object[] arr = field.GetCustomAttributes(typeDescription, true);
                        if (arr.Length > 0)
                        {
                            DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                            strText = aa.Description;
                        }
                        else
                        {
                            strText = field.Name;
                        }
                        nvc.Add(strValue, strText);
                    }
                }
                return nvc;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 得到值对应的中文状态描述
        /// </summary>
        /// <param name="intKey">状态值</param>
        /// <returns></returns>
        public static string GetDescriptionByKey(Type enumType,int intKey)
        {
            Dictionary<string, string> list = GetNVCFromEnumValue(enumType);

            string strDes = string.Empty;
            list.TryGetValue(intKey.ToString(), out strDes);
            return strDes;
        }
    }

}
posted @ 2010-05-18 12:51 DOGNET 阅读(29) 评论(0) 编辑

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace DogNet.Common.CacheService
{
    public class DataAccessCached
    {
        private static object markAllEmployee = new object();
        /// <summary>
        /// 得到所有的员工(带缓存)
        /// </summary>
        /// <returns></returns>
        public static IList<object> GetAllEmployees()
        {
            string key = "AllEmployee";
            int cacheTime = Convert.ToInt32(ConfigurationManager.AppSettings["secondOfoutTime"]);
            IList<object> listResult = HttpRuntime.Cache[key] as List<object>;
            if (listResult == null)
            {
                lock (markAllEmployee)
                {
                    if (listResult == null)
                    {
                        //从DB或其它持久访问层得到需要缓存的数据
                        //JDHrmDB hrDB = new JDHrmDB();
                        //listResult = hrDB.GetAllEmployee();
                        HttpRuntime.Cache.Add(key, listResult, null, DateTime.Now.AddSeconds(cacheTime)
                            , Cache.NoSlidingExpiration, CacheItemPriority.High, new CacheItemRemovedCallback(CacheItemRemovedCallback));
                    }
                }
            }
            return listResult;
        }

        static void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
        {
            if (key == "AllEmployee")
            {
                System.Threading.Thread th = new System.Threading.Thread(ReLoadAllEmployees);
                th.Start();
            }
        }

        public static void ReLoadAllEmployees()
        {
            try
            {
                GetAllEmployees();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
posted @ 2010-05-18 11:07 DOGNET 阅读(100) 评论(0) 编辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace DogNet.Common.AppConfig
{
    /// <summary>
    /// 提供所有Xml配置文件的内存对象
    /// </summary>
    public static class XmlDataSource
    {
        #region 私有字段
        private static XmlFileConfig _AllComDeptConfig;
        #endregion

        #region 构造函数
        static XmlDataSource()
        {
            //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase) + "\\Config\\";
            //path = path.Replace("file:\\", "");

            string path = HttpContext.Current.Request.PhysicalApplicationPath + "Config\\";
            _AllComDeptConfig = new XmlFileConfig(path, "AllComDept.xml");
        }
        #endregion


        #region 公有属性
        /// <summary>
        /// 所有公司级部门
        /// </summary>
        public static XDocument AllComDeptConfig
        {
            get
            {
                return _AllComDeptConfig.XmlDocument;
            }
        }

        #endregion
    }
}

posted @ 2010-05-18 10:39 DOGNET 阅读(83) 评论(0) 编辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace DogNet.Common.AppConfig

{

     /// <summary>
    /// Xml配置文件加载,监听
    /// </summary>

    public class XmlFileConfig
    {
        private string _fullPath;
        private object _lockObject;
        private XDocument _xmlDocument;

        #region 构造函数
        public XmlFileConfig(string path, string name)
        {
            this._fullPath = Path.Combine(path, name);
            this._lockObject = new object();

            // 添加监视
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.Filter = name;
            watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;
            watcher.Changed += new FileSystemEventHandler(WatchHandler_OnChanged);
            watcher.EnableRaisingEvents = true;
        }
        #endregion

        public XDocument XmlDocument
        {
            get
            {
                if (_xmlDocument == null)
                {
                    lock (_lockObject)
                    {
                        if (_xmlDocument == null)
                        {
                            this._xmlDocument = LoadConfig();
                        }
                    }
                }
                return _xmlDocument;
            }
        }

        private XDocument LoadConfig()
        {
            try
            {
                return XDocument.Load(this._fullPath);
            }
            catch
            {
                return this._xmlDocument;
            }
        }

        private void WatchHandler_OnChanged(object source, FileSystemEventArgs e)
        {            
            lock (this._lockObject)
            {
                this._xmlDocument = LoadConfig();
            }
        }
    }
}
posted @ 2010-05-18 10:32 DOGNET 阅读(84) 评论(0) 编辑