Write a web custom control with C #

xiaoxiao2021-03-06  41

The original user control, but maintenance is too trouble, then write a custom control, better maintenance, easy to use. This control is a time control, selecting time, month, day, hour, minute, and second by drop-down list box. Atternuration, provide the starting year of the optional time. The source code is as follows: use system; use system.Web.ui; use system.Web.ui.WebControls; use system.componentmodel; using system.componentmodel.design;

Namespace userTime {///

/// User Custom Time Control, taken from year, month, day, hour, minute, second /// [DefaultProperty), ToolboxData ("< {0}: clsusertime runat = server> ") // InamingContainer provides a new naming range for the child control, guarantee the ID uniqueness of the child control PUBLIC CLSUSERTIME: System.Web.ui .WebControls.WebControl, INamingContainer {private System.Web.UI.WebControls.DropDownList ddlYear; private DropDownList ddlMonth; private DropDownList ddlDay; private DropDownList ddlHour; private DropDownList ddlMinite; private DropDownList ddlSecond;

Private static int basedear = 0; // The beginning of the year, here must use Static to prevent two-time calls After the variable is cleared private static bool isinit = false; // Is it already initialized, here must use Static to prevent secondary After calling the variable is cleared

Public int year // Take the control time: year {get {return ddlyear.selectedindex beginyear;}}

Public int MONTH / / TEATION Time: Month {Get {Return Ddlmonth.SelectedIndex 1;}}

Public Int day // Time: Daily {Get {Return DDLDAY.SELECTEDINDEX 1;}}

Public int hour // Time: {get {return;}}}

Public int minite // Take the control time: points {get {return ddlminite.selectedIndex;}}

PUBLIC INT Second // Take Control Time: Second {Get {Return Ddlsecond.selectedIndex;}}

// Use the initialization of the initialization control, if you have already initialized the control, return to public void initcontrols (Int Startyear, int endyear {if (isinit) {return;} ENSURECHILDCONTROLS (); if (Startyear> endyear) {Int TMP = startyear; startyear = endyear; endyear = startyear;} beginyear = startyear; for (int i = startyear; i <= endyear; i) {ddlyear.items.add (i.tostring ());} for (int);} for i = 1; i <= 12; i) {ddlmonth.Items.add (i.tostring ());} for (int i = 1; i <= 31; i) {ddlday.Items.add (i.tostring ());} for (int i = 0; i <24; i) {ddlhour.items.add (i.tostring ());} for (int i = 0; i <60; i) {ddlminite.Items.add (i.tostring ()); ddlsecond.Items.add (i.tostring ());} isinit = true;} // Reserved Controls property, gets the controlcollection object, the object represents the child controls the UI server control hierarchy of public override ControlCollection controls {get {// determine if the server contains child controls EnsureChildControls (); return base.Controls;}} // Create dynamic child controls protected override void CreateChildControls () { // Create a year, month, day, time, minute, second drop-down list Box this.Controls.clear (); ddlyear = new dropdownload (); ddlyear.id = "ddlyear"; ddlyear.width = 54;

DDLMONTH = New DropdownList (); ddlmonth.id = "ddlmonth"; ddlmonth.width = 40;

DDLDAY = New DropdownList (); ddlday.id = "ddlday"; ddlday.width = 40;

DDLHOUR = New DropdownList (); ddlhour.id = "ddlhour"; ddlhour.width = 40; ddlminite = new DropdownList (); ddlminite.id = "ddlminite.width = 40;

Ddlsecond = new DropDownList (); ddlsecond.id = "ddlsecond"; ddlsecond.width = 40; this.controls.add (ddlyear); this.controls.add (ddlmonth); this.controls.add (ddlday); this. Controls.add (ddlhour); this.controls.add (ddlminite); this.controls.add (ddlsecond);

Base.createchildControls ();

///

/// presented this control to the specified output parameter. /// /// To write HTML writer protected override void render (htmlTextWriter Output) {// will be added to the specified System.Web.htmlTextWritRibuteStorender (Output); Output.addattribute (System.Web.ui.htmlTextWriterattribute.cellpadding, "0", FALSE);

// Develop a table, put the child control on one row in one line Output.Renderbegintag (htmltextwritertag.table); // Table Output.Renderbegintag (htmlTextwritertag.tr); //

// Year drop list box Output.renderbegintag (htmlTextWritrtag.td); // Column DDLYEAR.RENDERCONTROL (OUTPUT); OUTPUT.RENDERENDTAG ();

Output.renderbegintag (HTMLTextWritertag.td); Output.write ("year"); output.renderendtag ();

// Month drop list box Output.renderbegintag (HTMLTextWritertag.td); ddlmonth.renderControl (Output); Output.renderendtag ();

Output.renderbegintag (HTMLTextWritertag.td); Output.write ("Month"); Output.renderendtag ();

// Japan drop-down list box Output.renderbegintag (htmlTextwritertag.td); ddlday.renderControl (Output); output.renderendtag ();

Output.renderbegintag (HTMLTextWritertag.td); Output.write ("Day"); Output.renderendtag ();

// The drop list box Output.renderbegintag (htmlTextWritrtag.td); ddlhour.renderControl (Output); Output.renderendtag ();

Output.renderbegintag (HTMLTextWritertag.td); Output.write ("Time"); Output.renderendtag ();

// points drop-down list box output.RenderBeginTag (HtmlTextWriterTag.Td); ddlMinite.RenderControl (output); output.RenderEndTag (); output.RenderBeginTag (HtmlTextWriterTag.Td); output.Write ( "points"); output.RenderEndTag ( );

// Second drop-down list box output.renderbegintag (htmlTextwritertag.td); ddlsecond.renderControl (Output.renderendtag ();

Output.renderbegintag (HTMLTextWritertag.td); Output.write ("second"); output.renderendtag ();

Output.renderendtag (); output.renderendtag ();

Public datetime gettime () // Get selected time {datetime time = new datetime (this.year, this.month, this.day, this.Hour, this.minite, this.second, 0); returnit;}

// Clear all the values ​​of all drop-down list boxes in the control (DDLMONTH.ITEMS.CLEAR (); DDLMINITEMS.CLEAR (); DDLMINITEMS.CLEAR (); ddlhour.items.clear (); ddlMinite .Items.clear (); ddlsecond.Items.clear (); isinit = false;

// Undo option on all public void ClearSelection drop-down list box () {ddlYear.ClearSelection (); ddlMonth.ClearSelection (); ddlDay.ClearSelection (); ddlHour.ClearSelection (); ddlMinite.ClearSelection (); ddlSecond.ClearSelection ( }}}

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

New Post(0)