ASP.NET (3) - User/Custom Controls

This series of articles is about ASP.NET. They are from my previous years, legacy notes on OneNote. Reviewing the concepts may be helpful even for our current programming because they have a lot of similar features, such as Angular, React, and these modern "laguanges".

A. Introduction

This article will be an ASP.NET User Control Summary. I will just follow the exiting note without editing. It is quite informative.

  • A - Introduction
  • B - ASP.NET User/Custom controls:
    • User Control
    • Custom Control

B. ASP.NET User/Custom Controls

  • user control (within an app.),
  • custom Control/Rendered Control (shared by different apps.)
  • Dynamic controls: build on the fly

C. User Controls

A user control consists of a user interface page (with the extension .ascx) and the code-behind code. A user control is consumed by a .aspx file or another user control and must be included in the same project with its client. User interface rendering is done automatically using the .ascx page. A user control can contain any number of other user controls or custom Web controls.

  • User controls are contained in a .ascx file;
  • User control’s class is declared as an abstract class and must be used as a base class for a derived class;
  • User controls cannot contain <html>, <body>, or <form> HTML tags;
  • User controls cannot contain the @Page directive;
  • User control’s Page_Load runs after the web form’s Page_Load.
  • In a sense, user controls are the ASP.NET counterpart of classic ASP, including files, except that they can encapsulate only user interface elements (no code element or procedure in a user control is accessible from the client .aspx page).

 D. Custom Controls

Custom Web controls can be shipped as compiled assemblies. User interface rendering must be done manually through code. Custom Web controls inherit from system.web.UI.Webcontrols (or control) for their user interface, and then extend the functionality of their parent.

  • Full custom control (Rendered custom control): created from the scratch;
    • Inherits System.Web.UI.WebControls.WebControl;
    • Render manually by code through the HtmlTextWriter class;
    • Handling Postback Data:
      • Preserve data between callbacks by ViewState collection
      • Implement the IPostBackDataHandler interface to receive a notification when a postback occurs.
    • Raising server-side events (handled by calling page) …
    • Generating a PostBack Event: use Page.GetPostBackEventReference
    • Handle PostBack Events: Implements IPostBackEventHandler interface
  • Derived custom control: Extend an existing control
  • Composite custom control: group an existing controls together
    • Override the CreateChildrenControl method of the base class to instantiate all the constituent controls and add them to its own control collection.
    • Implement the InamingContainer interface to inform ASP.NET that this control has child controls and each control must receive a unique ID.


Similar Articles