ASP. NET component design step by step (2)

Source: Internet
Author: User

From the inheritance relationship perspective,Asp.netThe categories of server controls are roughly divided4Class:

1,User Control

SimilarPage, Basically no programming is required.AspxThe page is used as a reusable component.

 

2,Literalcontrol

ASP. NETAdd allHtmlExample of compiling element and readable text into this class. For example, the start tag does not containRunat = "server"Attribute/Value PairHtmlThe element is compiledLiteralcontrolObject.

The behavior of the text control is the same as that of the text control, which means that the text can be extracted from the text control andControlsAttribute from the parent Server Control'sControlcollectionTo remove the text control. ThereforeLiteralcontrolWhen exporting a custom control, make sure that the control executes any required preprocessing steps, instead of usingLiteralcontrol. RenderCall methods to implement them. This is generally done to improveWebApplicationProgramResponse time.

LiteralcontrolGenerally, server events are not triggered.

It can be used in programmingControlcollection. AddOrControlcollection. RemoveTo add or remove text controls from pages or server controls.

 

3,Htmlcontrol

HtmlElementAsp.netComponent of the model server. EveryHtmlcontrolDirectly corresponds to a specificHtmlElement (not necessarily an input element ).

4,Webcontrol

NormallyWebControl. It can be understood as complicatedHtmlElements and server-side processing logic.

 

Inheritance relationship diagram:

System. Object

System. Web. UI. Control

System. Web. UI. templatecontrol

System. Web. UI. Page

System. Web. UI. usercontrol

System. Web. UI. literalcontrol

System. Web. UI. htmlcontrol

System. Web. UI. webcontrol

 

The user control,WebControl.Xxx

SlaveWebcontrolInheritedWebServer Components often inherit a wealthUIElement and control capability.

 

User Control

1,Why is it a user control?

Modern people prefer to be lazy, "What you see is what you get", and "drag-and-drop" programming. InAsp.net WebProjects are often shared among multiple pages.UIFor exampleHeader footerIf the content of all pages is consistentJSFile, but if you needUISome may vary according to different user statuses. This requires dynamic logic processing.JS/htmlYou cannot meet the requirements. BeforeASPIn useInclude ASPFile, nowAsp.netChanged includeASPThe user control is used to solve all kinds of file defects.

User Controls are essentially independentAsp.netFile with the extensionAscx. User Controls usually depend on specificAsp.net WebProject. When a user control is used,Asp.netPage parser fromAspxDynamically generate a class in the file and compile it into a specific assembly (hosted in temporary cache)DLL), And follow. NetAnd process the instance.

 

 

2,How to obtain user controls

Obtain the user control inVSIn fact, you only need:

InWebAdd Project-> User Control-> Enter the user control name.-> On the blank pageUIDesign and storage.

Drag and Drop the user control inWebForm to use the user control. In this caseVSInstead, we did the following:

AddRegisterCommand. Specifically:

<% @ Register tagprefix = "uc1" tagname = "webusercontrol1" src = "webusercontrol1.ascx" %>The page interpreter can be interpreted:<Uc1: webusercontrol1As a user control,Uc1Label prefix (similar to StandardWebControlASPTag prefix ),Webusercontrol1As the label of the user control, this combination is interpreted as using the user control instance.SRCThe value is the virtual path of the user control source file, but cannot be an absolute path.

 

3,The essence of User Controls (Asp.netBackground of the user control)

Asp.netIs compiled. processing user controls is similar to processing pages. All elements on pages are compiled into specific classes.Asp.netPage calls and triggers events. User Controls are also reused in this way. When processing user controls, the page framework performs the following steps:

ExplanationAscxFile, which is interpreted as a derived fromSystem. Web. UI. usercontrolManaged classes

Dynamically compile to auto-generated accessories.

The above process only occurs when the user control is used for the first time. Subsequent reuse of this user control will omit this step. Page usageRegisterCommand to reference this class. User Controls on the page will become instances of this type. In fact, user controls are also compiledAsp.netClass, derived fromSystem. Web. UI. usercontrolClass, the page uses these classes as if to useAsp.netThe standard class is different in that the standard control class can be directly instantiated, and the user control needs to compile similar pages before use, classes can be instantiated after being compiled and assembled into accessories.

 

4,User Control Programming considerations

User components cannot passNewBecause the user control class is dynamically generated,NewThe user control class does not exist. However, you can usePage. loadcontrol (string controlname)To loadLoadcontrolYou can find the user control, you need to useRefenceCommand:<% @ Refence control = "xxx/usercontrolsamples. ascx" %>.

User Controls can be nested by other user controls, but must be correctly referenced.

The user control itself belongsUI, Can be cached,PassAscxAt the top of the fileOutputcacheCommand to determine.

User ControlCodeToURLIt refers to the path where the user control exists, rather than calling the Page code path of the user control.Page. resolveurlCorrect resolution.

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.