Differences between user control and custom control
Web user controls and custom controls are designed to achieve code reuse, so that program developers can easily and quickly develop and improve development efficiency.
Although Web user controls and custom controls are implemented to achieve code reuse, they have their own characteristics and are suitable for use in different scenarios. Developers must be accurate in development, these two controls are used efficiently. A reasonable choice should be made based on the differences between the two.
Web user controls are easy to create, but they may not be easy to use in advanced solutions. The method for developing Web user controls is almost the same as that for developing Web forms pages. Similar to Web forms, user controls can be created in the visualization designer, written using code isolated from HTML, and can process execution events. However, since Web user controls are dynamically compiled at runtime, they cannot be added to the toolbox, and they are represented by a simple placeholder flag when added to the page. If you are used to fully support Visual Studio. NET design (including "property" window and "design" view Preview), this will make Web user controls more inconvenient to use. In addition, the only way to share a user control between applications is to put a separate copy into each application. Therefore, if the control is changed, more maintenance is required.
Web custom controls are compiled code, which makes Web custom controls easier to use but more difficult to create. Web custom controls must be created using code. Once this control is created, you can add it to the toolbox and support it in the window with full "properties" and ASP.. NET Server controls are displayed in the visual designer for all other design-time functions. In addition, you can install a single copy of the Web custom control in the Global Assembly Cache and share the copy among applications, which makes maintenance easier. For more information, see Global Assembly cache.
If your control contains a large number of static la S, the user control may be a better choice. If your control is mainly generated dynamically (for example, the row of the Data Binding table, the node of the Tree View, or the tab of the tab control), the custom control may be a better choice.
The following table lists the main differences between the two types:
Web user control |
Web Custom Controls |
Easy to create |
Difficult to create |
Limited support for users using Visual Design Tools |
Provide users with full visual design tool support |
Each application requires a separate copy of the control. |
Only a single copy of the control is required in the Global Assembly Cache |
The Toolbox cannot be added to Visual Studio. |
Toolbox that can be added to Visual Studio |
Suitable for static Layout |
Suitable for Dynamic Layout |
(Note: The user control extension is. ascx. Create and use a page file and backend CS file similar to. aspx, And the custom control extension is. CS as a single file)
A Web user control is similar to a complete Web form page, which contains a user interface page and a code hidden file. The User Interface page is different from the. aspx file in the following aspects: the user control extension must be. ascx.
User Controls do not contain <HTML>, <BODY>, and <FORM> elements (these elements must be on the home page ).
In other aspects, user controls are similar to Web forms pages. You can use the same HTML elements and Web controls as those on standard Web forms pages. For example, if you want to create a user control that will be used as a toolbar, you can place a series of "Button" Web server controls on the control, and create Event Handlers for these buttons.