zoukankan      html  css  js  c++  java
  • 专用于ASP.Net Web应用程序的日期控件

     原文引入:http://blog.csdn.net/nileel/article/details/1566051

    专用于ASP.Net Web应用程序的日期控件

    分类: ASP.NET/C# 518人阅读 评论(0) 收藏 举报

    由3个dropdownlist组成的选择日期控件
    可以设置相应属性

    代码如下:

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.Design.WebControls;
    using System.Web.UI.WebControls;

    namespace AppCode
    {
        /// <summary>
        
    /// AspNetDate 选择输入日期控件
        
    /// </summary>

        [DefaultProperty("Text"), 
        ParseChildren(false),
        PersistChildren(false),
        Description("专用于ASP.Net Web应用程序的日期控件"),
        Designer(typeof(DateDesigner)),
        ToolboxData("<{0}:WSCDate runat=server></{0}:WSCDate>")]
        public class WSCDate:Panel,INamingContainer,IPostBackDataHandler
        {
            #region 属性
            /// <summary>
            
    /// 获取/设置日期值。
            
    /// </summary>

            [Bindable(true),
            Browsable(true),
            Description("日期值"),
            Category("外观"), 
            DefaultValue("")] 
            public string Text 
            {
                get
                {
                    if (ViewState["Text"] != null)
                    {
                        return ViewState["Text"].ToString();
                    }

                    else
                    {
                        if (IsNull)
                        {
                            return "";
                        }

                        else
                        {
                            DateTime date=System.DateTime.Today;
                            string str="";
                            switch (DateFormat)
                            {
                                case "YMD":
                                    str=date.ToString("yyyy-MM-dd",System.Globalization.DateTimeFormatInfo.InvariantInfo);
                                    break;
                                case "YM":
                                    str=date.ToString("yyyy-MM",System.Globalization.DateTimeFormatInfo.InvariantInfo);
                                    break;
                                case "Y":
                                    str=date.Year.ToString();
                                    break;
                            }

                            return str;
                        }

                    }

                }


                set
                {
                    if (value=="")
                    {
                        ViewState["Text"] = "";
                    }

                    else if (DateFormat=="YMD")
                    {
                        DateTime date;
                        try
                        {
                            date=Convert.ToDateTime(value);
                        }

                        catch
                        {
                            date=System.DateTime.Today;
                        }

                        string str = date.ToString("yyyy-MM-dd",System.Globalization.DateTimeFormatInfo.InvariantInfo);
                        if (str=="1900-01-01")
                            str="";
                        ViewState["Text"] =str;
                    }

                    else
                    {
                        ViewState["Text"] = value;
                    }

                }

            }

            /// <summary>
            
    /// 获取/设置日期值是否允许空。
            
    /// </summary>

            [Browsable(true), 
            Description("日期值是否允许空"),
            Category("布局"), 
            DefaultValue(false)] 
            public bool IsNull
            {
                get
                {
                    return (ViewState["IsNull"]==null)?false:true;
                }

                set
                {
                    if (value)
                        ViewState["IsNull"]=true;
                }

            }

            /// <summary>
            
    /// 获取/设置日期值格式(YMD:年-月-日 YM:年-月 Y:年)。
            
    /// </summary>

            [Browsable(true), 
            Description("日期值格式(YMD:年-月-日 YM:年-月 Y:年)"),
            Category("布局"), 
            DefaultValue("YMD")] 
            public string DateFormat
            {
                get
                {
                    return (ViewState["DateFormat"]==null)?"YMD":(string)ViewState["DateFormat"];
                }


                set
                {
                    ViewState["DateFormat"]=value;
                }

            }

            /// <summary>
            
    /// 获取/设置日期值能否编辑。
            
    /// </summary>

            [Browsable(true), 
            Description("能否编辑"),
            Category("行为"), 
            DefaultValue(true)] 
            public override bool Enabled
            {
                get
                {
                    return (ViewState["Enabled"]==null)?true:false;
                }


                set
                {
                    if (!value)
                        ViewState["Enabled"]=false;
                }

            }

            /// <summary>
            
    /// 获取/设置日期值中可供选择的年份长度。
            
    /// </summary>

            [Browsable(true), 
            Description("日期值中可供选择的年份长度"),
            Category("布局"), 
            DefaultValue(100)] 
            public int Length
            {
                get
                {
                    object obj=ViewState["Length"];
                    return (obj==null)?100:(int)obj;
                }


                set
                {
                    ViewState["Length"]=value;
                }

            }

            /// <summary>
            
    /// 获取/设置选择年份的结束值。
            
    /// </summary>

            [Browsable(true), 
            Description("日期值中选择结束年份,当小于100时表示距今年数"),
            Category("布局"), 
            DefaultValue(0)] 
            public int End
            {
                get
                {
                    object obj=ViewState["End"];
                    int y;
                    if (obj==null)
                    {
                        y=System.DateTime.Today.Year;
                    }

                    else
                    {
                        y=(int)obj;
                        if (y<100)
                        {
                            y=System.DateTime.Today.Year+y;
                        }

                    }

                    return y;
                }


                set
                {
                    ViewState["End"]=value;
                }

            }

            /// <summary>
            
    /// 获取选择年份的开始值。
            
    /// </summary>

            [Browsable(false),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public int Start
            {
                get{return End-Length;}
            }


            #endregion

            重写事件

        }



        #region 控件设计器
        /// <summary>
        
    /// 服务器控件设计器。
        
    /// </summary>

        public class DateDesigner:System.Web.UI.Design.WebControls.PanelDesigner
        {
            /// <summary>
            
    /// 初始化 PagerDesigner 的新实例。
            
    /// </summary>

            public DateDesigner()
            {
                this.ReadOnly=true;
            }

            private WSCDate wb;

            /// <summary>
            
    /// 获取用于在设计时表示关联控件的 HTML。
            
    /// </summary>
            
    /// <returns>用于在设计时表示控件的 HTML。</returns>

            public override string GetDesignTimeHtml()
            {

                wb=(WSCDate)Component;
                wb.Text="";
                StringWriter sw=new StringWriter();
                HtmlTextWriter writer=new HtmlTextWriter(sw);
                wb.RenderControl(writer);
                return sw.ToString();
            }


            /// <summary>
            
    /// 获取在呈现控件时遇到错误后在设计时为指定的异常显示的 HTML。
            
    /// </summary>
            
    /// <param name="e">要为其显示错误信息的异常。</param>
            
    /// <returns>设计时为指定的异常显示的 HTML。</returns>

            protected override string GetErrorDesignTimeHtml(Exception e)
            {
                string errorstr="创建控件时出错:"+e.Message;
                return CreatePlaceHolderDesignTimeHtml(errorstr);
            }

        }

        #endregion

    }

    使用方法:在asp.net 页中加入

    <%@ Register TagPrefix="cc1" Namespace="AppCode" Assembly="AppCode" %>

    <cc1:wscdate id="WSCDate1" runat="server" IsNull="False" Length="46" End="2050" Wrap="False"></cc1:wscdate>

  • 相关阅读:
    ABP 数据库 -- ABP&EF中的多表、关联查询
    C# List集合基础操作
    C# ABP 允许跨域请求
    异或运算、与运算、或运算 运用在 多项选择题
    C# ABP 配置连接数据库&创建表
    C# ABP WebApi与Swagger UI的集成
    C# 深入了解泛型
    8、SpringBoot+Mybatis整合------参数取值方式
    7、SpringBoot+Mybatis整合------PageHelper简单分页
    6、SpringBoot+Mybatis整合------参数传递
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3396885.html
Copyright © 2011-2022 走看看