ASP.NET - Directives - Control

In ASP.NET, the "control" directive is used to add a user-defined control to a web page. A user-defined control is a custom server control that encapsulates a piece of functionality and can be used across multiple web pages.

The syntax of the "control" directive is as follows:

<%@ Control ClassName="ClassName" %>

Using a custom user control:

<%@ Control ClassName="CustomUserControl" %>
<%@ Register TagPrefix="custom" TagName="CustomUserControl" Src="~/CustomUserControl.ascx" %>
<custom:CustomUserControl runat="server" />

In this example, a custom user control with the class name "CustomUserControl" is added to the web page. The control is defined in the "CustomUserControl.ascx" file, which is located in the root of the web application.

Using a custom server control:

<%@ Control ClassName="CustomServerControl" %>
<%@ Register TagPrefix="custom" Namespace="MyCustomControls" Assembly="MyCustomControls" %>
<custom:CustomServerControl runat="server" Text="Hello, World!" />

In this example, a custom server control with the class name "CustomServerControl" is added to the web page. The control is defined in the "MyCustomControls" namespace and assembly. The "Text" property of the control is set to "Hello, World!".

Using a custom composite control:

<%@ Control ClassName="CustomCompositeControl" %>
<%@ Register TagPrefix="custom" Namespace="MyCustomControls" Assembly="MyCustomControls" %>
<custom:CustomCompositeControl runat="server">
    <Items>
        <custom:CustomCompositeControlItem Text="Item 1" Value="1" />
        <custom:CustomCompositeControlItem Text="Item 2" Value="2" />
        <custom:CustomCompositeControlItem Text="Item 3" Value="3" />
    </Items>
</custom:CustomCompositeControl>

In this example, a custom composite control with the class name "CustomCompositeControl" is added to the web page. The control contains multiple "CustomCompositeControlItem" child controls, each with a "Text" and "Value" property.