Custom Controls

Custom Controls are controls that are created by combining two or more controls and can be reused in multiple Web applications.

Following types of custom controls can be created:-

    • Web user controls

    • Web custom controls

    • Templated user controls

Web User Controls

Web user controls are containers that can be created by combining one or more Web server controls. After creating a Web user control, it can can be treated as a unit and define properties and methods for it. Web user controls are similar to ASP.NET Web pages in the context that they contain both a user interface page and code.

A Web user control is created in the same way as you create an ASP.NET page. However it differs from an ASP.NET Web page in the following ways:

    • The file name extension of a user control is .ascx.

    • A user control contains the @Control directive instead of @Page directive.

    • User controls cannot run as stand-alone files. They need to be added to ASP.NET pages to make them work.

    • User controls do not have <html> <body> or <form> elements in them. These elements must be present on the Web page that is hosting these controls.

A Web user control on a Web Page must be registered before it is used. A Web user control can be registered by using @Register directive.

Web Custom controls

Web custom controls provide an approach to reuse logic in an ASP.NET application. Web custom controls are written as classes. Web custom controls are written entirely by using managed code and have no markup files. These controls are compiled into an assembly before the deployment of the application.

Web custom controls are the following three types:-

    • Controls that combine two or more existing controls.

    • Controls that extend the functionality of an existing controls.

    • Controls that are not similar to any existing control.

Templated User Controls

A templated user control allows developers to modify the layout of its user interface by defining their own templates. They allow the separation of control data from its presentation. They do not have a user interface; instead they simply implement a naming container and include a class whose properties and methods are accessible to the host page. The user interface of the control is defined at the time of designing the Web page on which the templated control is to be used.