C#自定义日历控件

 
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
 {
     /** 
     /// 日历控件
     /// 
     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 新增属性
             /** 
             /// 定义枚举变量
             /// 
             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            /** 
104            /// 开放出来text控件,用于server端取值
105            /// 
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            /** 
169            /// 定义控件内容
170            /// 
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("
")); 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(" 
200
")); 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("
")); 233 //加入js事件用于点其它地方时,隐藏日历 234 this.Controls.Add(new LiteralControl("")); 237 } 238 catch (Exception exception2) 239 { 240 throw exception2; 241 } 242 } 243 /** 244 /// 实现接口function 245 /// 246 /// 247 /// 248 /// 249 public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) 250 { 251 return true; 252 } 253 /** 254 /// 实现接口function 255 /// 256 public virtual void RaisePostDataChangedEvent() 257 { 258 try 259 { 260 } 261 catch (Exception exception2) 262 { 263 throw exception2; 264 } 265 } 266 /** 267 /// 重载funciton 268 /// 269 /// 270 protected override void Render(HtmlTextWriter writer) 271 { 272 this.EnsureChildControls(); 273 base.Render(writer); 274 } 275 /** 276 /// 重载function 277 /// 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 /** 292 /// 日期button事件 293 /// 294 /// 295 /// 296 void _btnDate_Click(object sender, ImageClickEventArgs e) 297 { 298 this._calDate.Visible = true; 299 } 300 /** 301 /// 选定日期function 302 /// 303 /// 304 /// 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 /** 321 /// 转换日期格式 322 /// 323 /// 要转日期 324 /// 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 350 351 352 353 354 355 356

 

你可能感兴趣的:(日历控件,日历,控件,C#,日历,c#,exception,string,textbox,calendar)