封装日期选择器控件
共有如下几个属性:
Minyear:最小年份
Maxyear:最大年份
Theme:样式
Valign:显示位置

用Calendar1.Text来取值。
代码如下
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Web;
5 using System.Drawing;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.HtmlControls;
9
10 namespace Fting.Calendar
11 {
12 [ToolboxData("<{0}:calendar runat=\"server\"></{0}:calendar>")]
13 public class calendar : TextBox
14 {
15 private int _minyear = 1900;
16 private int _maxyear = 2999;
17
18 [Browsable(true)]
19 [Description("最小年份")]
20 [DefaultValue("1900")]
21 [Category("设定")]
22 public virtual int Minyear
23 {
24 get { return _minyear; }
25 set { _minyear = value; }
26 }
27 [Browsable(true)]
28 [Description("最大年份")]
29 [DefaultValue("2999")]
30 [Category("设定")]
31 public virtual int Maxyear
32 {
33 get { return _maxyear; }
34 set { _maxyear = value; }
35 }
36 [Browsable(true)]
37 [Description("日历显示位置")]
38 [DefaultValue("Br")]
39 [Category("设定")]
40 public calendarvalign Valign
41 {
42 get
43 {
44 if (this.ViewState["valign"] == null)
45 {
46 return calendarvalign.bottom;
47 }
48 return (calendarvalign)this.ViewState["valign"];
49 }
50 set
51 {
52 this.ViewState["valign"] = value;
53 }
54 }
55
56 [Browsable(true)]
57 [Description("日历样式设置")]
58 [DefaultValue(0)]
59 [Category("设定")]
60 public calendarstyle Theme
61 {
62 get
63 {
64 if (this.ViewState["calendarstyle"] == null)
65 {
66 return calendarstyle.blue;
67 }
68 return (calendarstyle)this.ViewState["calendarstyle"];
69 }
70 set
71 {
72 this.ViewState["calendarstyle"] = value;
73 }
74 }
75
76 protected override void OnPreRender(EventArgs e)
77 {
78 string valign = null;
79 if (Valign.ToString() == "top")
80 {
81 valign = "Tr";
82 }
83 else if (Valign.ToString() == "bottom")
84 {
85 valign = "Br";
86 }
87 this.Page.PreRenderComplete += new EventHandler(Page_PreRender);
88 this.Attributes.Add("onFocus", "return getcalendar('" + this.ID + "','" + this.ID + "','" + valign + "','" + Minyear + "','" + Maxyear + "')");
89 base.OnPreRender(e);
90 }
91 void Page_PreRender(object sender, EventArgs e)
92 {
93 Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Fting.Calendar.javascript.calendar.js");
94 Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Fting.Calendar.javascript.cn_utf8.js");
95 Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Fting.Calendar.javascript.calendar-setup.js");
96 RegCss("Fting.Calendar.style." + Theme + ".css");
97 }
98 /// <summary>
99 /// 注册CSS样式表
100 /// </summary>
101 /// <param name="Path"></param>
102 private void RegCss(string Path)
103 {
104 HtmlGenericControl css = new HtmlGenericControl("link");
105 css.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(calendar), Path));
106 css.Attributes.Add("type", "text/css");
107 css.Attributes.Add("rel", "stylesheet");
108 Page.Header.Controls.Add(css);
109 }
110 }
111 public enum calendarstyle
112 {
113 blue,
114 brown,
115 green,
116 system,
117 tas,
118 win2k,
119 win2k_cold
120 }
121 public enum calendarvalign
122 {
123 top,
124 bottom
125 }
126 }
浙公网安备 33010602011771号