自定义日历控件
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web.UI;
5
using System.Web.UI.WebControls;
6
using System.Web.UI.HtmlControls;
7
using System.Drawing;
8
using System.Collections;
9
using System.Collections.Specialized;
10
using System.ComponentModel;
11
/**//*
12
* * ExpentDate:日期控件
13
* * Writor:Susan
14
* * DateTime:2006/12/18
15
*/
16
namespace CtrolBox
17

{
18
/**//// <summary>
19
/// 日历控件
20
/// </summary>
21
public class ExpentDate: WebControl, INamingContainer,IPostBackDataHandler
22
{
23
控件属性#region 控件属性
24
private TextBox _EditBox;
25
private Calendar _calDate;
26
private ImageButton _btnDate;
27
public int _Columns=15;
28
public TextStyle _Mode;
29
public string _ImageUrl = "Images/Calendar.jpg";
30
public bool _ReadOnly = true;
31
#endregion
32
属性定义#region 属性定义
33
新增属性#region 新增属性
34
/**//// <summary>
35
/// 定义枚举变量
36
/// </summary>
37
public enum TextStyle
38
{
39
point,
40
line,
41
Midline,
42
standard
43
}
44
[Bindable(true),
45
Category("Appearance"),
46
DefaultValue("Point"),
47
Description("指定日期显示样式:point:.;line:/;midline:-;standard:xxxx年xx月xx日")]
48
public TextStyle Mode
49
{
50
get
51
{
52
return _Mode;
53
}
54
set
55
{
56
_Mode = value;
57
}
58
}
59
[Bindable(true),
60
Category("Appearance"),
61
DefaultValue("15"),
62
Description("指定日期显示框的长度")]
63
public int Columns
64
{
65
get
66
{
67
return _Columns;
68
}
69
set
70
{
71
_Columns = value;
72
}
73
}
74
[Bindable(true),
75
Category("Appearance"),
76
DefaultValue("True"),
77
Description("指定日期显示框的可读性")]
78
public bool txtReadOnly
{
79
get
80
{
81
return _ReadOnly;
82
}
83
set
84
{
85
_ReadOnly = value;
86
}
87
}
88
[Bindable(true),
89
Category("Appearance"),
90
DefaultValue("Images/Calendar.jpg"),
91
Description("指定日期按钮所用图样")]
92
public virtual string ImageUrl
93
{
94
get
95
{
96
return _ImageUrl;
97
}
98
set
99
{
100
_ImageUrl = value;
101
}
102
}
103
/**//// <summary>
104
/// 开放出来text控件,用于server端取值
105
/// </summary>
106
[Bindable(true),
107
Category("Appearance"),
108
DefaultValue(""),
109
Description("文本框选中值")]
110
public string SelectedText
{
111
get
112
{
113
return _EditBox.Text;
114
}
115
set
116
{
117
_EditBox.Text = value;
118
}
119
}
120
private string _OnClientChangedText="";
121
[Bindable(true),
122
Category("Appearance"),
123
DefaultValue(""),
124
Description("文本框Client事件")]
125
public string OnClientChangedText
126
{
127
get
128
{
129
return _OnClientChangedText;
130
}
131
set
132
{
133
_OnClientChangedText = value;
134
}
135
}
136
private string _OnClientChangedKey="onchange";
137
[Bindable(true),
138
Category("Appearance"),
139
DefaultValue(""),
140
Description("文本框Client事件类型")]
141
public string OnClientChangedKey
142
{
143
get
144
{
145
return _OnClientChangedKey;
146
}
147
set
148
{
149
_OnClientChangedKey = value;
150
}
151
}
152
#endregion
153
公开属性#region 公开属性
154
[Bindable(true),
155
Category("Appearance"),
156
DefaultValue(""),
157
Description("文本框的事件")]
158
public event System.EventHandler TextChanged;
159
public void OnTextChanged(Object Sender, EventArgs e)
160
{
161
if (TextChanged != null)
162
TextChanged(this, e);
163
}
164
#endregion
165
#endregion
166
控件实现#region 控件实现
167
重载函数#region 重载函数
168
/**//// <summary>
169
/// 定义控件内容
170
/// </summary>
171
protected override void CreateChildControls()
172
{
173
try
174
{
175
this.Controls.Clear();
176
//添加第一个控件:textbox控件;
177
this._EditBox = new TextBox();
178
this._EditBox.TextChanged += new EventHandler(OnTextChanged);
179
this._EditBox.Attributes.Add(this._OnClientChangedKey, this._OnClientChangedText);
180
this._EditBox.Columns =this._Columns;
181
this._EditBox.ID = "EditBox";
182
this._EditBox.ReadOnly = this._ReadOnly;
183
this._EditBox.Text = fn_getDate(DateTime.Now.ToString("yyyy-MM-dd"));
184
this.Controls.Add(new LiteralControl("<table><tr><td valign=\"Top\"
185
width=\"100%\">"));
186
this.Controls.Add(this._EditBox);
187
//添加第二个参数,标志位
188
HtmlInputHidden hidonClickFlg = new HtmlInputHidden();
189
hidonClickFlg.ID = "hidonClickFlg";
190
hidonClickFlg.Value = "Y";
191
this.Controls.Add(hidonClickFlg);
192
//加入操作button
193
this._btnDate = new ImageButton();
194
this._btnDate.ID = "btnDate";
195
this._btnDate.ImageUrl = this._ImageUrl;
196
this._btnDate.Click += new ImageClickEventHandler(_btnDate_Click);
197
this.Controls.Add(this._btnDate);
198
//加入日历
199
this.Controls.Add(new LiteralControl(" </td></tr></table>
200
<div id='divextenddate' style='POSITION:absolute;display:;'>"));
201
//整体设置
202
this._calDate = new Calendar();
203
this._calDate.ID = "calDate";
204
this._calDate.BackColor = Color.White;
205
this._calDate.ShowGridLines = true;
206
this._calDate.BorderColor = Color.DodgerBlue;
207
this._calDate.BorderStyle = BorderStyle.Double;
208
this._calDate.Font.Size = 10;
209
this._calDate.Font.Name = "Verdana";
210
this._calDate.ForeColor = Color.Black;
211
this._calDate.Visible = false;
212
//设置title
213
this._calDate.TitleStyle.BackColor = Color.DodgerBlue;
214
this._calDate.TitleStyle.ForeColor = Color.White;
215
//设置星期
216
this._calDate.DayHeaderStyle.BackColor = Color.WhiteSmoke;
217
this._calDate.DayHeaderStyle.ForeColor = Color.Black;
218
//设置上下月
219
this._calDate.NextPrevFormat = NextPrevFormat.CustomText;
220
this._calDate.NextPrevStyle.ForeColor = Color.White;
221
this._calDate.NextPrevStyle.Font.Size = 8;
222
//设置其它的:当前日期,选中日期,不是本月的日期
223
this._calDate.OtherMonthDayStyle.ForeColor = Color.DarkGray;
224
this._calDate.TodayDayStyle.BackColor = Color.Coral;
225
this._calDate.TodayDayStyle.ForeColor = Color.White;
226
this._calDate.SelectedDayStyle.BackColor = Color.CornflowerBlue;
227
this._calDate.SelectedDayStyle.ForeColor = Color.White;
228
//添加事件
229
this._calDate.SelectionChanged += new EventHandler(_calDate_SelectionChanged);
230
this._calDate.Attributes.Add("onclick", "document.all." + hidonClickFlg.ClientID + ".value='Y';");
231
this.Controls.Add(this._calDate);
232
this.Controls.Add(new LiteralControl("</div>"));
233
//加入js事件用于点其它地方时,隐藏日历
234
this.Controls.Add(new LiteralControl("<SCRIPT event=onclick() for=document>if(document.all." +
235
hidonClickFlg.ClientID + ".value!='Y'){divextenddate.style.display='none';}else{document.all." +
236
hidonClickFlg.ClientID + ".value='';divextenddate.style.display='';}</SCRIPT>"));
237
}
238
catch (Exception exception2)
239
{
240
throw exception2;
241
}
242
}
243
/**//// <summary>
244
/// 实现接口function
245
/// </summary>
246
/// <param name="postDataKey"></param>
247
/// <param name="postCollection"></param>
248
/// <returns></returns>
249
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
250
{
251
return true;
252
}
253
/**//// <summary>
254
/// 实现接口function
255
/// </summary>
256
public virtual void RaisePostDataChangedEvent()
257
{
258
try
259
{
260
}
261
catch (Exception exception2)
262
{
263
throw exception2;
264
}
265
}
266
/**//// <summary>
267
/// 重载funciton
268
/// </summary>
269
/// <param name="writer"></param>
270
protected override void Render(HtmlTextWriter writer)
271
{
272
this.EnsureChildControls();
273
base.Render(writer);
274
}
275
/**//// <summary>
276
/// 重载function
277
/// </summary>
278
public override void DataBind()
279
{
280
try
281
{
282
base.DataBind();
283
}
284
catch (Exception exception2)
285
{
286
throw exception2;
287
}
288
}
289
#endregion
290
一般函数#region 一般函数
291
/**//// <summary>
292
/// 日期button事件
293
/// </summary>
294
/// <param name="sender"></param>
295
/// <param name="e"></param>
296
void _btnDate_Click(object sender, ImageClickEventArgs e)
297
{
298
this._calDate.Visible = true;
299
}
300
/**//// <summary>
301
/// 选定日期function
302
/// </summary>
303
/// <param name="sender"></param>
304
/// <param name="e"></param>
305
void _calDate_SelectionChanged(object sender, EventArgs e)
306
{
307
if (this._calDate.SelectionMode.Equals(CalendarSelectionMode.Day))
308
{
309
string strdate=this._calDate.SelectedDate.ToString();
310
strdate = strdate.Substring(0, strdate.IndexOf(":") - 2);
311
this._EditBox.Text = fn_getDate(strdate);
312
this._calDate.Visible = false;
313
//加载事件
314
OnTextChanged(sender, e);
315
}
316
else
{
317
this._calDate.Visible = true;
318
}
319
}
320
/**//// <summary>
321
/// 转换日期格式
322
/// </summary>
323
/// <param name="strdate">要转日期</param>
324
/// <returns></returns>
325
protected string fn_getDate(string strdate)
{
326
switch (this._Mode)
327
{
328
case TextStyle.line:
329
strdate = strdate.Replace("-", "/");
330
break;
331
case TextStyle.point:
332
strdate = strdate.Replace("-", ".");
333
break;
334
case TextStyle.Midline:
335
strdate = strdate.Replace("-", "-");
336
break;
337
default:
338
strdate = (strdate.Split("-".ToCharArray()))[0] + "年" + (strdate.Split("-".ToCharArray()))[1] + "月" +
339
strdate.Split("-".ToCharArray())[2] + "日";
340
break;
341
}
342
return strdate;
343
}
344
#endregion
345
#endregion
346
}
347
}
348
//注册到专案中(web.config)
349
<pages>
350
<controls>
351
<!-- 注册自定义控件 -->
352
<add tagPrefix="ppmis" namespace="CtrolBox" assembly="CtrolBox"/>
353
<!-- 注册用户控件 -->
354
<add tagPrefix="ppmis" src="~/Controls/UnitCtl.ascx" tagName="UnitCtl"/>
355
</controls>
356
</pages>
357
358
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Web.UI;5
using System.Web.UI.WebControls;6
using System.Web.UI.HtmlControls;7
using System.Drawing;8
using System.Collections;9
using System.Collections.Specialized;10
using System.ComponentModel;11

/**//*12
* * ExpentDate:日期控件13
* * Writor:Susan14
* * DateTime:2006/12/1815
*/16
namespace CtrolBox17


{18

/**//// <summary>19
/// 日历控件20
/// </summary>21
public class ExpentDate: WebControl, INamingContainer,IPostBackDataHandler22

{23

控件属性#region 控件属性24
private TextBox _EditBox;25
private Calendar _calDate;26
private ImageButton _btnDate;27
public int _Columns=15;28
public TextStyle _Mode;29
public string _ImageUrl = "Images/Calendar.jpg";30
public bool _ReadOnly = true;31
#endregion32

属性定义#region 属性定义33

新增属性#region 新增属性34

/**//// <summary>35
/// 定义枚举变量36
/// </summary>37
public enum TextStyle38

{39
point,40
line,41
Midline,42
standard43
}44
[Bindable(true),45
Category("Appearance"),46
DefaultValue("Point"),47
Description("指定日期显示样式:point:.;line:/;midline:-;standard:xxxx年xx月xx日")]48
public TextStyle Mode49

{50
get51

{52
return _Mode;53
}54
set55

{56
_Mode = value;57
}58
}59
[Bindable(true),60
Category("Appearance"),61
DefaultValue("15"),62
Description("指定日期显示框的长度")]63
public int Columns64

{65
get66

{67
return _Columns;68
}69
set70

{71
_Columns = value;72
}73
}74
[Bindable(true),75
Category("Appearance"),76
DefaultValue("True"),77
Description("指定日期显示框的可读性")]78

public bool txtReadOnly
{79
get80

{81
return _ReadOnly;82
}83
set84

{85
_ReadOnly = value;86
}87
}88
[Bindable(true),89
Category("Appearance"),90
DefaultValue("Images/Calendar.jpg"),91
Description("指定日期按钮所用图样")]92
public virtual string ImageUrl93

{94
get95

{96
return _ImageUrl;97
}98
set99

{100
_ImageUrl = value;101
}102
}103

/**//// <summary>104
/// 开放出来text控件,用于server端取值105
/// </summary>106
[Bindable(true),107
Category("Appearance"),108
DefaultValue(""),109
Description("文本框选中值")]110

public string SelectedText
{111
get112

{113
return _EditBox.Text;114
}115
set116

{117
_EditBox.Text = value;118
}119
}120
private string _OnClientChangedText="";121
[Bindable(true),122
Category("Appearance"),123
DefaultValue(""),124
Description("文本框Client事件")]125
public string OnClientChangedText126

{127
get128

{129
return _OnClientChangedText;130
}131
set132

{133
_OnClientChangedText = value;134
}135
}136
private string _OnClientChangedKey="onchange";137
[Bindable(true),138
Category("Appearance"),139
DefaultValue(""),140
Description("文本框Client事件类型")]141
public string OnClientChangedKey142

{143
get144

{145
return _OnClientChangedKey;146
}147
set148

{149
_OnClientChangedKey = value;150
}151
}152
#endregion153

公开属性#region 公开属性154
[Bindable(true),155
Category("Appearance"),156
DefaultValue(""),157
Description("文本框的事件")]158
public event System.EventHandler TextChanged;159
public void OnTextChanged(Object Sender, EventArgs e)160

{161
if (TextChanged != null)162
TextChanged(this, e);163
}164
#endregion165
#endregion166

控件实现#region 控件实现167

重载函数#region 重载函数168

/**//// <summary>169
/// 定义控件内容170
/// </summary>171
protected override void CreateChildControls() 172

{173
try174

{175
this.Controls.Clear();176
//添加第一个控件:textbox控件;177
this._EditBox = new TextBox();178
this._EditBox.TextChanged += new EventHandler(OnTextChanged);179
this._EditBox.Attributes.Add(this._OnClientChangedKey, this._OnClientChangedText);180
this._EditBox.Columns =this._Columns;181
this._EditBox.ID = "EditBox";182
this._EditBox.ReadOnly = this._ReadOnly;183
this._EditBox.Text = fn_getDate(DateTime.Now.ToString("yyyy-MM-dd"));184
this.Controls.Add(new LiteralControl("<table><tr><td valign=\"Top\"185
width=\"100%\">"));186
this.Controls.Add(this._EditBox);187
//添加第二个参数,标志位188
HtmlInputHidden hidonClickFlg = new HtmlInputHidden();189
hidonClickFlg.ID = "hidonClickFlg";190
hidonClickFlg.Value = "Y";191
this.Controls.Add(hidonClickFlg);192
//加入操作button193
this._btnDate = new ImageButton();194
this._btnDate.ID = "btnDate";195
this._btnDate.ImageUrl = this._ImageUrl;196
this._btnDate.Click += new ImageClickEventHandler(_btnDate_Click);197
this.Controls.Add(this._btnDate);198
//加入日历199
this.Controls.Add(new LiteralControl(" </td></tr></table>200
<div id='divextenddate' style='POSITION:absolute;display:;'>"));201
//整体设置202
this._calDate = new Calendar();203
this._calDate.ID = "calDate";204
this._calDate.BackColor = Color.White; 205
this._calDate.ShowGridLines = true;206
this._calDate.BorderColor = Color.DodgerBlue;207
this._calDate.BorderStyle = BorderStyle.Double;208
this._calDate.Font.Size = 10;209
this._calDate.Font.Name = "Verdana";210
this._calDate.ForeColor = Color.Black;211
this._calDate.Visible = false;212
//设置title213
this._calDate.TitleStyle.BackColor = Color.DodgerBlue;214
this._calDate.TitleStyle.ForeColor = Color.White;215
//设置星期216
this._calDate.DayHeaderStyle.BackColor = Color.WhiteSmoke;217
this._calDate.DayHeaderStyle.ForeColor = Color.Black;218
//设置上下月219
this._calDate.NextPrevFormat = NextPrevFormat.CustomText;220
this._calDate.NextPrevStyle.ForeColor = Color.White;221
this._calDate.NextPrevStyle.Font.Size = 8;222
//设置其它的:当前日期,选中日期,不是本月的日期223
this._calDate.OtherMonthDayStyle.ForeColor = Color.DarkGray;224
this._calDate.TodayDayStyle.BackColor = Color.Coral;225
this._calDate.TodayDayStyle.ForeColor = Color.White;226
this._calDate.SelectedDayStyle.BackColor = Color.CornflowerBlue;227
this._calDate.SelectedDayStyle.ForeColor = Color.White;228
//添加事件 229
this._calDate.SelectionChanged += new EventHandler(_calDate_SelectionChanged);230
this._calDate.Attributes.Add("onclick", "document.all." + hidonClickFlg.ClientID + ".value='Y';"); 231
this.Controls.Add(this._calDate);232
this.Controls.Add(new LiteralControl("</div>"));233
//加入js事件用于点其它地方时,隐藏日历234
this.Controls.Add(new LiteralControl("<SCRIPT event=onclick() for=document>if(document.all." +235
hidonClickFlg.ClientID + ".value!='Y'){divextenddate.style.display='none';}else{document.all." + 236
hidonClickFlg.ClientID + ".value='';divextenddate.style.display='';}</SCRIPT>"));237
}238
catch (Exception exception2)239

{240
throw exception2;241
}242
} 243

/**//// <summary>244
/// 实现接口function245
/// </summary>246
/// <param name="postDataKey"></param>247
/// <param name="postCollection"></param>248
/// <returns></returns>249
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)250

{251
return true;252
}253

/**//// <summary>254
/// 实现接口function255
/// </summary>256
public virtual void RaisePostDataChangedEvent()257

{258
try259

{260
}261
catch (Exception exception2)262

{263
throw exception2;264
}265
}266

/**//// <summary>267
/// 重载funciton268
/// </summary>269
/// <param name="writer"></param>270
protected override void Render(HtmlTextWriter writer)271

{272
this.EnsureChildControls();273
base.Render(writer);274
}275

/**//// <summary>276
/// 重载function 277
/// </summary>278
public override void DataBind()279

{280
try281

{282
base.DataBind();283
}284
catch (Exception exception2)285

{286
throw exception2;287
}288
}289
#endregion290

一般函数#region 一般函数291

/**//// <summary>292
/// 日期button事件293
/// </summary>294
/// <param name="sender"></param>295
/// <param name="e"></param>296
void _btnDate_Click(object sender, ImageClickEventArgs e)297

{298
this._calDate.Visible = true;299
}300

/**//// <summary>301
/// 选定日期function302
/// </summary>303
/// <param name="sender"></param>304
/// <param name="e"></param>305
void _calDate_SelectionChanged(object sender, EventArgs e)306

{307
if (this._calDate.SelectionMode.Equals(CalendarSelectionMode.Day))308

{309
string strdate=this._calDate.SelectedDate.ToString();310
strdate = strdate.Substring(0, strdate.IndexOf(":") - 2);311
this._EditBox.Text = fn_getDate(strdate);312
this._calDate.Visible = false;313
//加载事件314
OnTextChanged(sender, e);315
}316

else
{317
this._calDate.Visible = true;318
}319
}320

/**//// <summary>321
/// 转换日期格式322
/// </summary>323
/// <param name="strdate">要转日期</param>324
/// <returns></returns>325

protected string fn_getDate(string strdate)
{326
switch (this._Mode)327

{328
case TextStyle.line:329
strdate = strdate.Replace("-", "/");330
break;331
case TextStyle.point:332
strdate = strdate.Replace("-", ".");333
break;334
case TextStyle.Midline:335
strdate = strdate.Replace("-", "-");336
break;337
default:338
strdate = (strdate.Split("-".ToCharArray()))[0] + "年" + (strdate.Split("-".ToCharArray()))[1] + "月" +339
strdate.Split("-".ToCharArray())[2] + "日";340
break;341
}342
return strdate;343
}344
#endregion345
#endregion346
}347
}348
//注册到专案中(web.config)349
<pages>350
<controls>351
<!-- 注册自定义控件 -->352
<add tagPrefix="ppmis" namespace="CtrolBox" assembly="CtrolBox"/>353
<!-- 注册用户控件 -->354
<add tagPrefix="ppmis" src="~/Controls/UnitCtl.ascx" tagName="UnitCtl"/>355
</controls>356
</pages>357

358

浙公网安备 33010602011771号