asp.net.ph

Skip Navigation Links

Web Forms Control Identification

Web Forms Server Controls   Programming Web Forms Server Controls   Accessing Server Controls Programmatically


Every control on an ASP.NET Web page must be uniquely identifiable.

In general, assigning a value to a control’s ID property uniquely identifies a control. This value becomes the instance name of the control, or the name by which you can refer to the control object in code.

For example, setting the ID property of a Label control to myLabel lets you reference the control in code as myLabel, and allows programmatic access to the object’s member properties, methods, and events as follows:

myLable.Text = "Hello World!"

A number of data-bound controls, particularly controls that use templates, such as the DataList, Repeater, DataGrid, GridView, FormView, and DetailsView controls, host template objects that act as containers for other ( child ) controls. When these controls run, they generate multiple instances of the template object, for instance a DataListItem in the case of a DataList control, which in turn generate multiple instances of any child control defined in that template.

For example, if you create a DataList template with a Label control in it, when the page runs, there are as many instances of that Label control in the page as there are records in the DataList control’s data source.

Because controls can be instantiated multiple times on the same page, and because similar control names can be used on different pages, the ASP.NET page framework provides mechanisms to ensure that controls on the pages comprising an application have unique identifiers.

The framework also provides ways to find these individual controls so that you can manipulate them in your own code.

The Naming Container

Controls that can act as containers for other controls generate a naming container, or an ID namespace, for their child controls. By providing this naming container, controls can guarantee that ID attributes of their child controls are unique within the entire application.

When child controls are created at run time, the naming container is combined with the child control’s ID property to create the value of the UniqueID property of each child control. The UniqueID property therefore becomes a fully qualified identifier for a control, referencing its naming container as well the controls’ individual ID value.

For example, if you create a DataList template with a Label control in it, when the page runs, the multiple instances of the Label control are created within the naming container of the parent DataList control. If you view the source HTML of the generated Web Forms page, the UniqueID property of each Label control will reflect this namespace, whose format will be something like

myDataList_ctl01_myLabel, myDataList_ctl02_myLabel, ...

and so on.

NOTE: It is strongly advised not to write code that references controls using the value of the generated UniqueID property. The UniqueID property can be treated as a handle, for example by passing it to a process, but its value must not be relied upon as having a specific structure.

In addition to each container control providing a naming container for its child controls, the page itself also provides a naming container for all of its child controls. This creates a unique namespace within the application for all the controls on that page.

Using the NamingContainer Property

Child controls can reference their naming container via the NamingContainer property. This property returns an object of type Control that you can cast to the appropriate DataList control, DataListItem object, and so on.

Referencing the naming container is useful when you need access from a child control to a property of the container control. For example, in a handler for a child control’s DataBinding event, you can access the DataItem object by getting it from the naming container.

NOTE: The NamingContainer property does not necessarily reference the same control as the Parent property. For example, in a Repeater control, you might have an item template containing a table that in turn contains a Label control. The parent control of the label is a table cell ( for example, a HtmlTableCell object ), but its naming container is the RepeaterItem object, because it is the RepeaterItem that defines the namespace for the Label control, not the table.

See Also

Web Forms Control ID Resolution   Accessing a Control’s Naming Container



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note