Inherit from the development of the compositecontrol Server Control

Source: Internet
Author: User

The original name of compositecontrol development is the development of composite controls. I think there are still some routines that can be followed for the development of controls.

 

Define the server-side properties of the control

 

Code
///   <Summary>
/// Script path
///   </Summary>
[Bindable ( True ), Browsable ( True ), Defaultvalue ( " ../JS/SG/sg_autofilter.js " )]
Public   String Scriptpath
{
Get
{
If (Viewstate [ " Scriptpath " ] ! =   Null )
Return ( String ) Viewstate [ " Scriptpath " ];
Return   " ../JS/SG/sg_autofilter.js " ;
}
Set
{
Viewstate [ " Scriptpath " ] = Value;
}
}

 

Define the start label of the server control

 

Code
Protected OverrideHtmltextwritertag tagkey
{
Get
{
ReturnHtmltextwritertag. div;
}
}

 

Add style attributes to the start tag of the server control

 

Code
Protected Override VoidAddattributestorender (htmltextwriter writer)
{
Writer. addattribute (htmltextwriterattribute. style,String. Format ("Width: {0} PX", Width ));
}

 

Add sub-controls in the tag

 

Code
Protected   Override   Void Createchildcontrols ()
{
Private Htmlinputtext txttargetcontrol =   New Htmlinputtext ();
Private Hiddenfield txttargetcontrolvalue =   New Hiddenfield ();
Private System. Web. UI. webcontrols. Image imgpopup;

Controls. Clear ();
// Txttargetcontrol = new htmlinputtext ();
Txttargetcontrol. ID =   " Txttargetcontrol " ;
Txttargetcontrol. Value =   This . Text;
Txttargetcontrol. Attributes. Add ( " AutoComplete " , " Off " );
Txttargetcontrol. Attributes. Add ( " Style " , String . Format ( " Border: 0px solid #9 abfdc; width: {0} PX " ,( This . Width -   17 ). Tostring ()));
Imgpopup =   New System. Web. UI. webcontrols. Image ();
Imgpopup. ID =   " Imgpopup " ;
Imgpopup. style. Add (htmltextwriterstyle. cursor, " Hand " );
Imgpopup. Attributes. Add ( " Title " , " After selecting a company, click this button to close the company selection box! " );
If (Popupimageurl =   "" )
Imgpopup. imageurl = Page. clientscript. getwebresourceurl ( This . GetType (), " Sagacity.controls.popup.gif " );
Else
Imgpopup. imageurl = Popupimageurl;
// Txttargetcontrolvalue = new hiddenfield ();
Txttargetcontrolvalue. ID =   " Txttargetcontrolvalue " ;
Txttargetcontrolvalue. Value =   This . Value;
Controls. Add (txttargetcontrol );
Controls. Add (txttargetcontrolvalue );
Controls. Add (imgpopup );

Childcontrolscreated= True;

}

 

Add style attributes to controls in the tag

 

Code
Protected   Override   Void Rendercontents (htmltextwriter writer)
{
Writer. addattriding (htmltextwriterattribute. cellpadding, " 0 " );
Writer. addattriing (htmltextwriterattribute. cellspacing, " 0 " );
Writer. addattribute (htmltextwriterattribute. style, String . Format ( " Border: 1px solid #9 abfdc; width: {0} PX " , This . Width ));
Writer. renderbegintag (htmltextwritertag. Table );
Writer. renderbegintag (htmltextwritertag. tr );
Writer. addattriign (htmltextwriterattribute. Align, " Right " );
Writer. addstyleattribute (htmltextwriterstyle. paddingright, " 0px " );
Writer. renderbegintag (htmltextwritertag. TD );
Txttargetcontrol. rendercontrol (writer );
Txttargetcontrolvalue. rendercontrol (writer );
Writer. renderendtag ();
Writer. addattriign (htmltextwriterattribute. Align, " Left " );
Writer. addstyleattribute (htmltextwriterstyle. paddingleft, " 0px " );
Writer. addstyleattribute (htmltextwriterstyle. paddingright, " 0px " );
Writer. addstyleattribute (htmltextwriterstyle. Width, " 17px " );
Writer. renderbegintag (htmltextwritertag. TD );
Imgpopup. rendercontrol (writer );
Writer. renderendtag ();
Writer. renderendtag ();
Writer. renderendtag ();
}

 

Add event

 

Code
Protected   Static   Readonly   Object _ Click =   New   Object ();

Public EventEventhandler click
{
Add {events. addhandler (_ click, value );}
Remove {events. removehandler (_ click, value );}
}

 

This event is triggered at a certain time and handled, as shown in figure

 

Code
Protected   Virtual   Void Oninit (eventargs E)
{
Eventhandler inithandler = (Eventhandler) events [_ click];
If (Inithandler ! =   Null )
{
Raiseevents ( This , e);
}< BR >}< P>

private void raiseevents ( Object sender, eventargs E)
{< br> // Body Code
}

of course, if you want to make professional controls, there are still many issues to consider. Such as support during design, comparison of client JS writing, user experience, and so on are all issues that need to be considered

the general control development process is only mentioned here.

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.