DatePicker(日期选择自定义服务器控件)的实现

类别:.NET开发 点击:0 评论:0 推荐:

在DotNet中有用户自定义控件(.ascx)和服务器控件(程序代码)

WebControl 类提供所有 Web 服务器控件的公共属性、方法和事件。通过设置在此类中定义的属性,可以控制 Web 服务器控件的外观和行为。

例如在Community中DatePicker控件的实现

public class DatePicker : WebControl, INamingContainer {

         DropDownList dropMonth;

         DropDownList dropDay;

         DropDownList dropYear;

 

         public int Month {

              get {

                   if (ViewState["Month"] == null)

                       return DateTime.Now.Month;

                   else

                       return (int)ViewState["Month"];

              }

              set { ViewState["Month"] = value; }

         }

 

         public int Day {

              get {

                   if (ViewState["Day"] == null)

                       return DateTime.Now.Day;

                   else

                       return (int)ViewState["Day"];

              }

              set { ViewState["Day"] = value; }

         }

       

         public int Year {

              get {

                   if (ViewState["Year"] == null)

                       return DateTime.Now.Year;

                   else

                       return (int)ViewState["Year"];

              }

              set {

                   ViewState["Year"] = value;

              }

         }

 

         public DateTime Date {

              get {

                   //确定服务器控件是否包含子控件。如果不包含,则创建子控件。

                   EnsureChildControls();

               

                   int _year = Int32.Parse(dropYear.SelectedItem.Value);

                   int _month = Int32.Parse(dropMonth.SelectedItem.Value);

                   int _day = Int32.Parse(dropDay.SelectedItem.Value);

               

                   if (_day > DateTime.DaysInMonth(_year, _month) )

                       throw new ArgumentException("Invalid date!");

               

                   return new DateTime

                       (

                       _year,

                       _month,

                       _day

                       );

              }

              set {

                   Month = value.Month;

                   Day = value.Day;

                   Year = value.Year;

              }

         }

 

         /// <summary>

         /// 通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。

         /// 当开发复合服务器控件或模板服务器控件时,必须重写此方法。

         /// </summary>

         protected override void CreateChildControls() {

              dropMonth = new DropDownList();

              Controls.Add(dropMonth);

       

              dropDay = new DropDownList();

              Controls.Add(dropDay);

           

              dropYear = new DropDownList();

              Controls.Add(dropYear);

 

              if (!Page.IsPostBack) {

                   // Get a DateTimeFormatInfo object

                   DateTimeFormatInfo objDateInfo = DateTimeFormatInfo.CurrentInfo;

           

                   // Display Months

                   for (int i=1;i<objDateInfo.MonthNames.Length;i++)

                       dropMonth.Items.Add(new ListItem(objDateInfo.GetMonthName(i), i.ToString()));

 

                   // Display Days

                   for (int i=1;i<32;i++)

                       dropDay.Items.Add(i.ToString());

 

                   // Display Years

                   for (int i=DateTime.Now.Year - 5;i<DateTime.Now.Year+5;i++)

                       dropYear.Items.Add(i.ToString());

           

 

                   dropMonth.Items.FindByValue(Month.ToString()).Selected = true;

                   dropDay.Items.FindByValue(Day.ToString()).Selected = true;

                   dropYear.Items.FindByValue(Year.ToString()).Selected = true;

              }   

       

         }

 

         /// <summary>

         /// 将服务器控件内容发送到提供的 HtmlTextWriter 对象,此对象编写将在客户端呈现的内容。

         /// 在开发自定义服务器控件时,可以重写此方法以生成 ASP.NET 页的内容。

         /// </summary>

         /// <param name="tw">HtmlTextWriter 对象</param>

         protected override void Render(HtmlTextWriter tw) {

              // Get Date Parts

              string[] dateParts;

              try {

                   DateTimeFormatInfo objDateInfo = DateTimeFormatInfo.CurrentInfo;

                   //modify by yukh ,because in some objDateInfo.ShortDatePattern seperate by "-" eg: yyyy-M-d

                   if (objDateInfo.ShortDatePattern.IndexOf("/")>0)

                       dateParts = objDateInfo.ShortDatePattern.Split('/');

                   else

                       dateParts = objDateInfo.ShortDatePattern.Split('-');

 

              }

              catch {

                   dateParts = new string[]{"m","d","y"};

              }

              tw.RenderBeginTag(HtmlTextWriterTag.Table);

              tw.RenderBeginTag(HtmlTextWriterTag.Tr);

              tw.RenderBeginTag(HtmlTextWriterTag.Td);

              RenderDatePart(dateParts[0],tw);

              tw.RenderEndTag();

 

              tw.RenderBeginTag(HtmlTextWriterTag.Td);

              RenderDatePart(dateParts[1],tw);

              tw.RenderEndTag();

 

              tw.RenderBeginTag(HtmlTextWriterTag.Td);

              RenderDatePart(dateParts[2],tw);

              tw.RenderEndTag();

 

              tw.RenderEndTag(); // close row

              tw.RenderEndTag(); // close table

 

       

         }

 

         private void RenderDatePart(string datePart,HtmlTextWriter tw) {

              datePart = datePart.ToLower();

           

              if (datePart.StartsWith("m"))

                   dropMonth.RenderControl(tw);     //将服务器控件子级的内容输出到提供的 HtmlTextWriter 对象,

                                                    //此对象编写将在客户端呈现的内容。

              else

                   if (datePart.StartsWith("d"))

                   dropDay.RenderControl(tw);

              else

                   dropYear.RenderControl(tw);

         }

}

本文地址:http://com.8s8s.com/it/it44491.htm