How to Register Custom Controls?

A user control in ASP.NET is a powerful component that combines multiple web server controls and mark-up into a reusable unit. It functions similarly to an ASP.NET Web page, allowing you to define properties, methods, and events specific to the control. By encapsulating functionality within a user control, you can easily reuse it across multiple ASP.NET web pages, promoting code reusability and modular development. User controls provide a convenient way to create custom controls with their own distinct behavior and appearance, enhancing the flexibility and maintainability of your ASP.NET applications.

Register Custom Controls

You can register a custom server control to a Web page using the @Register directive. Create an @ Register directive that includes:

  1. A TagPrefix attribute, which associates a prefix with the user control. This prefix will be included in opening tag of the user control element.

  2. A TagName attribute, which associates a name with the user control. This name will be included in the opening tag of the user control element.

  3. A Src attribute, which defines the virtual path to the user control file that you are including.

syntax
< %@ Register TagPrefix="" TagName="" Src="" % >
example

The user control is in the file "uploader.ascx" in the Controls folder. In the page, the control is registered to use the prefix "up" and the tag name "Uploader".

< %@ Register TagPrefix="up" TagName="Uploader" Src="~/Controls/uploader.ascx" % >

The Src attribute value can be either a relative or an absolute path to the user control source file from your application's root directory.

How to use?

Within the body of an ASP.NET Web page, you can incorporate a user control element by declaring it inside the form element. If the user control exposes public properties, you have the option to set these properties declaratively, allowing you to define their values directly within the markup of the page. This provides a convenient way to configure the user control's behavior and appearance without requiring additional code. For example, the user control may include properties such as MinValue and MaxValue, which can be easily assigned specific values declaratively, simplifying the customization of the control's behavior.

Register Custom Controls in asp.net