思路:实现日期年月日的选择 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