原创控件代码共享--日期选择控件(一)

翻译|其它|编辑:郝浩|2005-08-29 15:01:00.000|阅读 1159 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


思路:实现日期年月日的选择

1、可以设定年的起止年份

2、排除不正确日期选择的可能

3、使用javascript实现控制

4、使用Text属性方便获取设置日期值

=================================

代码如下:
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 JSY
{

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

[DefaultProperty("Text"),
ParseChildren(false),
PersistChildren(false),
Description("专用于ASP.Net Web应用程序的日期控件"),
Designer(typeof(DateDesigner)),
ToolboxData("<{0}:JSYNetDate runat=server></{0}:JSYNetDate>")]
public class JSYNetDate: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>
/// 重写OnLoad 方法。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="EventArgs"/> 对象。</param>

protected override void OnLoad(EventArgs e)
{
if (Page.IsPostBack)
{
   string y=Page.Request.Form[this.UniqueID+"_year"];
   string m=Page.Request.Form[this.UniqueID+"_month"];
   string d=Page.Request.Form[this.UniqueID+"_day"];
   switch (DateFormat)
    {
       case "YMD":
       if (y=="" || m=="" || d=="")
       {
         Text="";
        }
        else
        {
          Text=y+"-"+m+"-"+d;
        }
      break;
      case "YM":
   if (y=="" || m=="")
    {
        Text="";
     }
   else
   {
      Text=y+"-"+m;
   }
   break;
case "Y":
   if (y=="")
    {
       Text="";
    }
  else
  {
      Text=y;
     }
   break;
   }
  }
base.OnLoad(e);
}

/// <summary>
/// 重写<see cref="System.Web.UI.WebControls.WebControl.AddAttributesToRender"/> 方法,验证是否有form(runat=server)控件
/// </summary>
/// <param name="writer"></param>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP