At times, you need to access properties or methods of a control's naming container. For example, during data binding, the naming container makes available a DataItem property containing the data to which controls are bound. You can access the containing control in different ways, depending on context.
To access the naming container from a data-binding expression
In the data-binding expression, use the
Containerkeyword, which returns a reference to the container. You can then access the container's properties or methods.
This keyword is used most commonly in the Eval method to get values from the naming container's object, but can be used outside of that method as well. The following example shows a control that might be in a template for a , , or control. It displays the current item number.
The following example is similar, but gets a value from the naming container's DataItem object:
The NamingContainer property does not necessarily reference the same control as the 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 object), but its naming container is the object, because it is the DataListItem that defines the namespace for the Label control, not the table.
To access the naming container from code
Get the control's property and cast it to the container's class type, such as .
For an example of using the NamingContainer property to get information about an individual control's naming container, see Using the NamingContainer Property to Determine a Control's Naming Container.