DatePicker (Date Select Custom server control) implementation

Source: Internet
Author: User
Server | control | Date selection has user-defined controls (. ascx) and server controls (program code) in dotnet

The WebControl class provides common properties, methods, and events for all WEB server controls. You can control the appearance and behavior of Web server controls by setting properties defined in this class.

For example, the implementation of DatePicker controls in community

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 {

Determines whether the server control contains child controls. If not, the child control is created.

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>

Notifies you to create any child controls that they contain by using a composite implementation-based server control to prepare for postback or rendering.

You must override this method when you are developing a composite server control or a template server control.

</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>

Sends the server control content to the provided HtmlTextWriter object, which writes what will be rendered on the client.

When you develop a custom server control, you can override this method to generate the contents of the ASP.net page.

</summary>

<param name= "TW" >htmltextwriter object </param>

protected override void Render (HtmlTextWriter tw) {

Get Date Parts

String[] dateparts;

try {

DateTimeFormatInfo objdateinfo = Datetimeformatinfo.currentinfo;

Modify by Yukh, because on 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); Outputs the contents of the server control's child to the provided HtmlTextWriter object.

This object writes the content that will be rendered on the client.

Else

if (Datepart.startswith ("D"))

Dropday.rendercontrol (TW);

Else

Dropyear.rendercontrol (TW);

}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.