DATEPICKER (Date Select Custom Server Control)

xiaoxiao2021-03-06  104

There is a user-defined control (.ascx) and server control (program code) in DOTNET

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

For example, 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] == 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 {

/ / Determine if the server control contains the child control. If not included, create a child control.

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;

}

}

///

/// Notifys the use of the composite-based server control to create any sub-controls they contain to prepare for the return or presentation.

/// The method must be rewritten when developing a composite server control or template server control.

///

Protected Override Void CreateChildControls () {

Dropmonth = new DropdownList ();

Controls.add (dropmonth);

DropDay = new dropdownload ();

Controls.add (dropday);

Dropyear = new DropdownList ();

Controls.add (Dropyear); if (! Page.ispostback) {

// Get a datetimeformatinfo objectinfo Object

DateTimeFormatinfo objdateInfo = datetimeformatinfo.currentinfo;

// Display Months

For (int i = 1; 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

Dropyear.Items.Add (i.tostring ());

DropMonth.ItemUE (Month.Tostring ()). SELECTED = true;

DropDay.Items.FindByValue (DAY.TOString ()). SELECTED = true;

Dropyear.items.FindByValue (Year.Tostring ()). SELECTED = true;

}

}

///

/// Send the server control content to the supplied HTMLTextWriter object, this object writes the content that will be rendered on the client.

/// When developing a custom server control, you can rewrite this method to generate the contents of the ASP.NET page.

///

/// HTMLTextWriter object

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); // output the contents of the server control sub-level 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);

}

}

转载请注明原文地址:https://www.9cbs.com/read-126321.html

New Post(0)