Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

ASP.NET controls - How controls are related to each other

Rate me:
Please Sign up or sign in to vote.
3.78/5 (16 votes)
14 Mar 2007CPOL3 min read 34.8K   19   6
This is another small article on my series about ASP.NET controls, and its main focus is to reveal how controls are related to each other, which entities are involved, and what their main roles in this task are.

Introduction

This is another small article on my series about ASP.NET controls, and its main focus is to reveal how controls are related to each other, which entities are involved, and what are their main roles in this task.

Motivation

Understanding the small pieces of magic that happen underneath our noose every time a Page request is handled and someone gets your web form in his browser will help you avoid many problems, to debug them faster, and to produce better applications.

If you're an experienced programmer, this knowledge may also help you to orchestrate complex workarounds and perhaps step through some of the ASP.NET constraints.

How controls are related to each other?

In order to expose an ASP.NET form, you need to place it in a Page, in fact, in any ASP.NET Control. In order to participate in the Page lifecycle, you need to be somehow related to the Page. This relation is achieved by a parent/children hierarchical structure.

The Page control is the root control of this hierarchical relationship, and is the one that handles which pipeline should be triggered (initial request, a postback request, or a callback request).

A control can, if needed, contain other controls, and all those child controls are kept together in a ControlCollection instance that is exposed through the Controls property. Every control knows its Parent Control.

A control that can contain other controls can also be marked as a participant in the automatic naming generation process of its descendents. This role is achieved by implementing the INamingContainer interface, and when this happens, the control is said to be a NamingContainer. Every child control will know its NamingContainer.

The ControlCollection provides the usual set of methods for collection management:

  • Add - Adds the specified Control object to the collection.
  • Remove - Removes the specified server control from the parent server control's ControlCollection object.
  • Clear - Removes all controls from the current server control's ControlCollection object.

A control can only be attached to another and become its child through the ControlCollection.Add method. Every time a control is added or removed from a collection, both its parent and its NamingContainer are notified and will react.

When a control is added to another control's children collection, the new parent Control.AddedControl method is invoked and the following steps are taken:

  1. Check if the newly added child control has an owner and therefore if its use is restricted only to that owner control. If true, the child is not a valid control and an exception is thrown.
  2. Check if the newly added child control belongs to another control's children collection, and in that case, the control will be removed from its previous parent's children collection. This is required to ensure a valid hierarchical relationship: a control can only belong to a single ControlCollection.
  3. Update the Page reference of the newly added child control to reference the new parent Page.
  4. Update the parent reference of the newly added child control to reference itself. If the new parent control is a NamingContainer, then we will update the NamingContainer reference of the newly added child control to reference itself.
  5. If the new child control ID has not been specified, then a new ID is automatically generated for the NamedControls collection of the new child control, and the NamingContainer is marked as dirty to ensure that a new and fresh collection is created and populated.
  6. If possible, the control life cycle is recreated for the newly added child control.

When a control is removed from its parent ControlCollection, the parent Control.RemovedControl method is invoked and the following steps are taken:

  1. Check if the newly added child control has an owner and therefore if its use is restricted only to that owner control. If true, the child is not a valid control and an exception is thrown.
  2. The NamedControls collection of the child control NamingContainer is marked as dirty to ensure that a new and fresh collection is created and populated (without the removed control).
  3. The child control performs an UnloadRecursive without disposing.
  4. All references set at AddedControl are removed and the control is released from any hierarchy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect everis
Portugal Portugal
Over 13 years of experience in the Software Development working mainly in the banking and insurance industry.

Over 3 year of experience as Operations Team Leader focused on Infrastructure Management and Software Configuration Management.

I've been honored with the Microsoft Most Valuable Professional (MVP) Award for three consecutive years, 2010, 2011 and 2012, in recognition to exceptional technical contributions and leadership.

Current / Recent Technical Projects
- Dominican Republic Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Colombian SECOPII Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Main Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Development ecosystem management, including Server management, , capacity management, SW monitoring and deploy management

Areas of Specialization:
- Operations Management - ISO 20000 & ISO 27001 driven
- Team Management and Coaching
- Technology Leadership, Solutions/Architecture
- Product life cycle management, Continuous Integration
- Technological background in Microsoft frameworks and tools.

Comments and Discussions

 
QuestionHow about retrieving child UserControl's data? Pin
xvietntx20-Mar-07 16:55
xvietntx20-Mar-07 16:55 
AnswerRe: How about retrieving child UserControl's data? Pin
Nuno M. F. Gomes22-Mar-07 14:25
Nuno M. F. Gomes22-Mar-07 14:25 
GeneralRe: How about retrieving child UserControl's data? Pin
xvietntx22-Mar-07 16:31
xvietntx22-Mar-07 16:31 
Questionhow about to Windows Controls? Pin
Southmountain14-Mar-07 6:18
Southmountain14-Mar-07 6:18 
AnswerRe: how about to Windows Controls? [modified] Pin
Nuno M. F. Gomes14-Mar-07 7:00
Nuno M. F. Gomes14-Mar-07 7:00 
GeneralRe: how about to Windows Controls? Pin
Paulo Morgado25-Mar-07 23:49
professionalPaulo Morgado25-Mar-07 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.