C#自定义日历控件

4 篇文章 0 订阅
2 篇文章 0 订阅
 
using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  using System.Drawing;
  using System.Collections;
  using System.Collections.Specialized;
 using System.ComponentModel;
 /**//*
  * * ExpentDate:日期控件
  * * Writor:Susan
  */
 namespace CtrolBox
 {
     /** <summary>
     /// 日历控件
     /// </summary>
     public class ExpentDate: WebControl, INamingContainer,IPostBackDataHandler
     {
         控件属性#region 控件属性
             private TextBox _EditBox;
             private Calendar _calDate;
             private ImageButton _btnDate;
             public  int  _Columns=15;
             public TextStyle _Mode;
            public string _ImageUrl = "Images/Calendar.jpg";
             public bool _ReadOnly = true;
         #endregion
         属性定义#region 属性定义
             新增属性#region 新增属性
             /** <summary>
             /// 定义枚举变量
             /// </summary>
             public enum TextStyle
             {
                 point,
                 line,
                 Midline,
                 standard
              }
             [Bindable(true),
              Category("Appearance"),
              DefaultValue("Point"),
              Description("指定日期显示样式:point:.;line:/;midline:-;standard:xxxx年xx月xx日")]
              public TextStyle Mode
             {
                  get
                  {
                      return _Mode;
                  }
                  set
                  {
                      _Mode = value;
                  }
              }
             [Bindable(true),
              Category("Appearance"),
              DefaultValue("15"),
              Description("指定日期显示框的长度")]
             public int Columns
            {
                 get
                 {
                     return _Columns;
                 }
                 set
                 {
                     _Columns = value;
                 }
             }
             [Bindable(true),
              Category("Appearance"),
              DefaultValue("True"),
              Description("指定日期显示框的可读性")]
             public bool txtReadOnly {
                 get
                 {
                     return _ReadOnly;
                 }
                 set
                 {
                     _ReadOnly = value;
                 }
             }
             [Bindable(true),
              Category("Appearance"),
             DefaultValue("Images/Calendar.jpg"),
             Description("指定日期按钮所用图样")]
             public virtual string  ImageUrl
             {
                 get
                 {
                     return _ImageUrl;
                 }
                 set
                 {
                    _ImageUrl = value;
                }
            }
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." + 
236hidonClickFlg.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] + "月" +
339strdate.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>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值