位置:海鸟网 > IT > ASP.NET >

刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(2)

//==========================================================================
//名称: ZYQ.WebControls.Cultural.DateTimePick.DateTimePickLabel
//       Asp.Net服务控件
//版本: 1.0.0.0
//作者: 张宇庆
//日期: 2003.2.12
//Email: raxzhang@sina.com
//说明: 本控件及源代码只是为《计算机世界》开发者俱乐部Asp.Net论坛学习如何开发Asp.net
//       服务器端控件而开发。未经本人同意请勿用作商业用途。
//
//==========================================================================
using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using ZYQ;
using ZYQ.WebControls.Cultural.DateTimePick;   
namespace ZYQ.WebControls.Cultural.DateTimePick
{
    
    /// <summary>
    /// DateTimePickLabel由System.Web.UI.WebControls.Label继承。
    /// 使用System.Globalization.CultureInfo类来显示格式化了的时间。
    /// </summary>
    [ToolboxData("<{0}:DateTimePickLabel runat=server></{0}:DateTimePickLabel>")]
    public class DateTimePickLabel : System.Web.UI.WebControls.Label
    {
        #region 私有变量定义
        private bool _autofit;
        private DateTime _dt;
        private string text;
        private DateFormatOption _dfo;
        private DTFormatSetting dateTimeSet;
        zyqCultrueInfo myInfo;
        #endregion
        #region 属性
        [Bindable(true),Browsable(false),
            Category("Appearance")]
        public override string Text
        {
            get
            {
                return text;
            }

//            set
//            {
//                text =value;        
//            }
        }
        [Browsable(true),Category("Behavior"),DefaultValue(false),Description("设定是否自动适应客户端浏览器的文化区域设定。如果此属性为true,则DateTimeFormatShow属性的所有设置将被忽略。")]
        public bool AutoFitClientBrowser
        {
            get{ return this._autofit;}
            set{this._autofit = value;}
        }

        [Browsable(true),Category("日期时间设置"),Description("设定日期"),Bindable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public DateTime DT
        {
            get
            {
                if(this._dt.ToString() != null)
                     return this._dt;
                return DateTime.Now;
            }
            set
            {
                this._dt =value;
                if(this.DateTimeFormatShow != null)
                    this.text=value.ToString(this.DateTimeFormatShow.FormateString,(new zyqCultrueInfo(this.DateTimeFormatShow.LanguageAndCountry,true)).DateTimeFormat);
            }
        }
        [Browsable(true),Category("日期时间设置"),TypeConverter(typeof(DTFormatSettingConverter)),DesignerSerializationVisibility(DesignerSerializationVisibility.Content),Description("设定时间和日期的格式")
        ,RefreshProperties(RefreshProperties.Repaint)]    
        public DTFormatSetting DateTimeFormatShow
        {
            get
            {
                if(this.dateTimeSet ==null)
                {
                    this.dateTimeSet= new DTFormatSetting();
                    this.dateTimeSet.DateTimeFormatChanged +=new DateTimeFormateEventHandler(OnTextChanged);                 
                    this.text=this.DT.ToString(this.dateTimeSet.FormateString,(new zyqCultrueInfo(this.dateTimeSet.LanguageAndCountry,true)).DateTimeFormat);
                }
                return this.dateTimeSet;
            }
            set
            {
                this.dateTimeSet=value;                
                this.dateTimeSet.DateTimeFormatChanged +=new DateTimeFormateEventHandler(OnTextChanged);                 
                this.text=this.DT.ToString(value.FormateString,(new zyqCultrueInfo(value.LanguageAndCountry,true)).DateTimeFormat);
            }
        }
        [Browsable(true),Category("日期时间设置"),Description("设定时间和日期的格式类型。此属性只在AutoFitClientBrowser属性为true时,才起作用。")]
        public DateFormatOption DateTimeOption
        {
            get{return this._dfo;}
            set
            {
                this._dfo = value;
                if(this.AutoFitClientBrowser)
                    if(this.Site !=null)
                        if(this.Site.DesignMode)
                            this.AutoFit(CultureInfo.CurrentCulture.Name.ToLower());  
            }
        }
        
        #endregion
        #region 构造
        public DateTimePickLabel():base()
        {
            myInfo=new zyqCultrueInfo(DateTimeFormatShow.LanguageAndCountry,true);
            _dt= DateTime.Now;
            this.Text=DT.ToString(this.DateTimeFormatShow.FormateString,myInfo.DateTimeFormat);
        }
        #endregion
        #region 重写相关受保护的函数和方法
        protected override void OnInit(EventArgs e)
        {            
            base.OnInit(e);
        }
        protected override void OnPreRender(EventArgs e)
        {    
            if(Page !=null)
            {
                if(this.AutoFitClientBrowser)
                {
                    this.AutoFit(this.Context.Request.UserLanguages[0].ToLower());      
//                    Page.Response.Charset =this.myInfo.Name;  
                    Page.Response.Write(this.Context.Request.UserLanguages[0]+"--"+this.myInfo.Name+"<br>");
                }
            }
            this.ToolTip =this.DateTimeFormatShow.FormateString+"(space,\".\",\"/\",\"-\",\":\")";
            this.text =this.DT.ToString(this.DateTimeFormatShow.FormateString,this.myInfo.DateTimeFormat);               
        }
        /// <summary>
        /// 将此控件呈现给指定的输出参数。
        /// </summary>
        /// <param name="output"> 要写出到的 HTML 编写器 </param>
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }
        #endregion
        #region 事件处理
        private void OnTextChanged(object Sender, FormatChangeEventArgs e)
        {
            try
            {
                if(this.myInfo !=null)
                    this.myInfo =null;
                this.myInfo = new zyqCultrueInfo(e.DTSetting.LanguageAndCountry,true);
                this.text =this.DT.ToString(e.DTSetting.FormateString,myInfo.DateTimeFormat );
            }
            catch(Exception ee)
            {
                Page.Response.Write(ee.Message+"<br>");
                this.myInfo=null;
                
            }
            finally
            {
            
            }
        }
        #endregion
        #region 私有方法和函数
        private void AutoFit(string Value)
        {
            if(this.myInfo !=null)
                this.myInfo =null;
            this.myInfo =new zyqCultrueInfo(Value,true);
            this.DateTimeFormatShow = new DTFormatSetting();
            this.DateTimeFormatShow.LanguageAndCountry = this.myInfo.Name.ToLower();                    
            this.DateTimeFormatShow.FormateString=this.myInfo.GetFormatString(this.DateTimeOption.ToString());
        }
        #endregion
    }
}