Using system; Using system. Web. UI; Using system. Web. UI. webcontrols; Using system. componentmodel; Using system. drawing; Namespace aquacalendar { [Defaultproperty ("text"), // default attribute displayed in the property toolbox Toolboxdata ("<{0}: datepicker runat = Server> </{0}: datepicker>")] Public class datepicker: system. Web. UI. webcontrols. webcontrol, ipostbackeventhandler { // Select the default style of the date button Private const string _ buttondefaultstyle = "border-Right: Gray 1px solid; border-top: Gray 1px solid; border-left: Gray 1px solid; cursor: hand; border-bottom: gray 1px solid ;"; // Button default text Private const string _ buttondefaulttext = "..."; Private system. Web. UI. webcontrols. Calendar _ calendar; Public override controlcollection controls { Get { Ensurechildcontrols (); // confirm that the child control set has been created Return base. controls; } } // Create a child control (server calendar Control) Protected override void createchildcontrols () { Controls. Clear (); _ Calendar = New Calendar (); _ Calendar. ID = mycalendarid; _ Calendar. selecteddate = datetime. parse (text ); _ Calendar. titleformat = titleformat. monthYear; _ Calendar. nextprevformat = nextprevformat. interval month; _ Calendar. cellspacing = 0; _ Calendar. Font. size = fontunit. parse ("9pt "); _ Calendar. Font. Name = "verdana "; _ Calendar. selecteddaystyle. backcolor = colortranslator. fromhtml ("#333399 "); _ Calendar. selecteddaystyle. forecolor = colortranslator. fromhtml ("white "); _ Calendar. daystyle. backcolor = colortranslator. fromhtml ("# cccccc "); _ Calendar. todaydaystyle. backcolor = colortranslator. fromhtml ("#999999 "); _ Calendar. todaydaystyle. forecolor = colortranslator. fromhtml ("Aqua "); _ Calendar. dayheaderstyle. Font. size = fontunit. parse ("8pt "); _ Calendar. dayheaderstyle. Font. Bold = true; _ Calendar. dayheaderstyle. Height = unit. parse ("8pt "); _ Calendar. dayheaderstyle. forecolor = colortranslator. fromhtml ("#333333 "); _ Calendar. nextprevstyle. Font. size = fontunit. parse ("8pt "); _ Calendar. nextprevstyle. Font. Bold = true; _ Calendar. nextprevstyle. forecolor = colortranslator. fromhtml ("white "); _ Calendar. titlestyle. Font. size = fontunit. parse ("12pt "); _ Calendar. titlestyle. Font. Bold = true; _ Calendar. titlestyle. Height = unit. parse ("12pt "); _ Calendar. titlestyle. forecolor = colortranslator. fromhtml ("white "); _ Calendar. titlestyle. backcolor = colortranslator. fromhtml ("#333399 "); _ Calendar. othermonthdaystyle. forecolor = colortranslator. fromhtml ("#999999 "); _ Calendar. nextprevformat = nextprevformat. customtext; _ Calendar. nextmonthtext = "Next Month "; _ Calendar. prevmonthtext = "last month "; _ Calendar. style. Add ("display", "NONE"); // The drop-down calendar control is not displayed by default. _ Calendar. selectionchanged + = new eventhandler (_ calendar_selectionchanged ); This. Controls. Add (_ calendar ); } [ Category ("appearance"), // category of this attribute, see Figure Defaultvalue (""), // default Attribute Value Description ("sets the date control value. ") // Attribute description ] Public String text { Get { Ensurechildcontrols (); Return (viewstate ["text"] = NULL )? System. datetime. Today. tostring ("yyyy-mm-dd"): viewstate ["text"]. tostring (); } Set { Ensurechildcontrols (); Datetime dt = system. datetime. today; Try { Dt = datetime. parse (value ); } Catch { Throw new argumentoutofrangeexception ("enter a date string (for example, 1981-04-29 )! "); } Viewstate ["text"] = dateformat = calendarenum. longdatetime? DT. tostring ("yyyy-mm-dd"): DT. tostring ("yyyy-m-d "); } } // Reload the enabled attribute of the server control to disable the date selection button) Public override bool Enabled { Get { Ensurechildcontrols (); Return viewstate ["enabled"] = NULL? True: (bool) viewstate ["enabled"]; } Set { Ensurechildcontrols (); Viewstate ["enabled"] = value; } } Public String buttonstyle { Get { Ensurechildcontrols (); Object o = viewstate ["buttonsytle"]; Return (O = NULL )? _ Buttondefaultstyle: O. tostring (); } Set { Ensurechildcontrols (); Viewstate ["buttonsytle"] = value; } } [ Defaultvalue (calendarenum. longdatetime ), ] Public calendarenum dateformat { Get { Ensurechildcontrols (); Object format = viewstate ["dateformat"]; Return format = NULL? Calendarenum. longdatetime :( calendarenum) format; } Set { Ensurechildcontrols (); Viewstate ["dateformat"] = value; Datetime dt = datetime. parse (text ); TEXT = dateformat = calendarenum. longdatetime? DT. tostring ("yyyy-mm-dd"): DT. tostring ("yyyy-m-d "); } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String mycalendarid // Compound Control ID { Get { Ensurechildcontrols (); Return this. clientid + "_ mycalendar "; } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String mycalendarname // Composite Control name { Get { Ensurechildcontrols (); Return this. uniqueid + ": mycalendar "; } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String datepickerinputid // ID of the input box in the Composite Control { Get { Ensurechildcontrols (); Return this. clientid + "_ dateinput "; } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String datepickerinputname // name of the input box in the Composite Control { Get { Ensurechildcontrols (); Return this. uniqueid + ": dateinput "; } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String datepickerbuttonid // the ID of the button in the Composite Control { Get { Ensurechildcontrols (); Return this. clientid + "_ datebutton "; } } [ Browsable (false ), Designerserializationvisibility (designerserializationvisibility. Hidden) ] Public String datepickerbuttonname // name of the button in the Composite Control { Get { Ensurechildcontrols (); Return this. uniqueid + ": datebutton "; } } Public String buttontext { Get { Ensurechildcontrols (); Return viewstate ["buttontext"] = NULL? _ Buttondefatext text :( string) viewstate ["buttontext"]; } Set { Ensurechildcontrols (); Viewstate ["buttontext"] = value; } } /// <Summary> /// Present the control to the specified output parameter. /// </Summary> /// <Param name = "output"> HTML writer to be written </param> Protected override void render (htmltextwriter output) { // When the control is output on the page, a table (two rows and two columns) is generated. The following is the table style. Output. addattriing (htmltextwriterattribute. cellspacing, "0 "); Output. addattribute (htmltextwriterattribute. Border, "0 "); Output. addattriding (htmltextwriterattribute. cellpadding, "0 "); Output. addstyleattribute ("Left", this. Style ["Left"]); Output. addstyleattribute ("TOP", this. Style ["TOP"]); Output. addstyleattribute ("position", "absolute "); If (width! = Unit. Empty) { Output. addstyleattribute (htmltextwriterstyle. Width, width. tostring ()); } Else { Output. addstyleattribute (htmltextwriterstyle. Width, "200px "); } Output. renderbegintag (htmltextwritertag. Table); // output table Output. renderbegintag (htmltextwritertag. tr); // the first row of the table Output. addattribute (htmltextwriterattribute. Width, "90% "); Output. renderbegintag (htmltextwritertag. TD ); // The attributes and style settings of the text box in the first column of the First row are as follows: If (! Enabled) { Output. addattribute (htmltextwriterattribute. readonly, "true "); } Output. addattribute (htmltextwriterattribute. type, "text "); Output. addattrid (htmltextwriterattribute. ID, datepickerinputid ); Output. addattribute (htmltextwriterattribute. Name, datepickerinputname ); Output. addattriter( htmltextwriterattribute. Value, text ); Output. addstyleattribute (htmltextwriterstyle. Width, "100% "); Output. addstyleattribute (htmltextwriterstyle. Height, "100% "); Output. addstyleattribute (htmltextwriterstyle. fontfamily, Font. Name ); Output. addstyleattribute (htmltextwriterstyle. fontsize, Font. Size. tostring ()); Output. addstyleattribute (htmltextwriterstyle. fontweight, Font. Bold? "Bold ":""); Output. addstyleattribute (htmltextwriterstyle. backgroundcolor, colortranslator. tohtml (backcolor )); Output. addstyleattribute (htmltextwriterstyle. Color, colortranslator. tohtml (forecolor )); Output. renderbegintag (htmltextwritertag. Input); // output text box Output. renderendtag (); Output. renderendtag (); Output. addattribute (htmltextwriterattribute. Width ,"*"); Output. renderbegintag (htmltextwritertag. TD ); // The following are the button attributes and style settings in the second column of the first row. If (! Enabled) { Output. addattribute (htmltextwriterattribute. Disabled, "true "); } Output. addattrimit (htmltextwriterattribute. type, "Submit "); Output. addattribute (htmltextwriterattribute. ID, datepickerbuttonid ); Output. addattribute (htmltextwriterattribute. Name, datepickerbuttonname ); Output. addattribute (htmltextwriterattribute. Value, buttontext ); Output. addstyleattribute (htmltextwriterstyle. Width, "100% "); Output. addattribute (htmltextwriterattribute. onclick, page. getpostbackeventreference (this); // when you click the button, you need to return it to the server to trigger the next onclick event. Output. addattribute (htmltextwriterattribute. style, buttonstyle ); Output. renderbegintag (htmltextwritertag. Input); // output button Output. renderendtag (); Output. renderendtag (); Output. renderendtag (); Output. renderbegintag (htmltextwritertag. tr ); Output. addattribute (htmltextwriterattribute. colspan, "2 "); Output. renderbegintag (htmltextwritertag. TD ); _ Calendar. rendercontrol (output); // outputs the calendar subcontrol. Output. renderendtag (); Output. renderendtag (); Output. renderendtag (); } // The Composite Control must inherit the ipostbackeventhandler interface to inherit the raisepostbackevent Public void raisepostbackevent (string eventargument) { Onclick (eventargs. Empty ); } Protected virtual void onclick (eventargs E) { // When you click the select date button, if the calendar subcontrol is not displayed, it is displayed and the value of the text box is assigned to the calendar subcontrol. If (_ calendar. attributes ["display"]! = "") { _ Calendar. selecteddate = datetime. parse (text ); _ Calendar. style. Add ("display ",""); } } // Date change event of the calendar control in the Composite Control Private void _ calendar_selectionchanged (Object sender, eventargs E) { // When the selected date changes, assign the selected date to the text box and hide the calendar subcontrol TEXT = _ calendar. selecteddate. tostring (); _ Calendar. style. Add ("display", "NONE "); } } } |