首页 > 编程 > .NET > 正文

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

2024-07-10 13:03:56
字体:
来源:转载
供稿:网友
//==========================================================================
//名称: zyq.webcontrols.cultural.datetimepick.datetimepicklabel
//       asp.net服务控件
//版本: 1.0.0.0
//作者: 张宇庆
//日期: 2003.2.12
//email: [email protected]
//说明: 本控件及源代码只是为《计算机世界》开发者俱乐部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
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表