Programming Microsoft ASP.NET 4 - Dino Esposito [117]
ID
Gets or sets the name that will be used to programmatically identify the control in the page.
NamingContainer
Gets a reference to the control’s naming container. The naming container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface. If no such control exists, the naming container is the host page.
Page
Gets a reference to the Page instance that contains the control.
Parent
Gets a reference to the parent of the control in the page hierarchy.
RenderingCompatibility
Indicates the version of ASP.NET that the rendered HTML of the control will be compatible with. This property requires ASP.NET 4.
Site
Gets information about the container that hosts the current control when rendered on a design surface. For example, you use this property to access the Visual Studio designer when the control is being composed in a Web form.
SkinID
Gets or sets the name of the skin to apply to the control. A skin is a particular subset of attributes in a theme.
TemplateControl
Gets a reference to the template that contains the current control.
TemplateSourceDirectory
Gets the virtual directory of the host page.
UniqueID
Gets a hierarchically qualified ID for the control.
ViewStateMode
Indicates how to treat the view state for the control regardless of the settings defined at the page level. This property requires ASP.NET 4.
Visible
Gets or sets whether ASP.NET has to render the control.
The Control class is the ideal base class for new controls that have no user interface and don’t require ASP.NET-based style information.
Important
As you can see in the preceding table, ASP.NET 4 still supports themes and skins. These are features through which you can style server controls using a fluent, .NET-based API. All in all, an ASP.NET theme is a superset of a cascading style sheet (CSS) and ultimately works by applying CSS styles to HTML elements being output by controls. Introduced with great pomp and ceremony, ASP.NET themes are today commonly deprecated in favor of plain HTML-level CSS styles.
Identifying a Server Control
A server control usually generates a piece of HTML markup. The root HTML element in the markup is always given a unique client-side ID. In ASP.NET 4, the client ID of a control can be generated in a number of different ways that I’ll cover in a moment.
In older versions, the client ID is always generated from the value of the UniqueID property—the truly server-side identifier that ASP.NET generates for each control. In versions of ASP.NET prior to version 4, the content of the ClientID property differs from UniqueID simply in that all occurrences of the dollar symbol ($), if any, are replaced with the underscore (_). Note that dollar symbols in the UniqueID string are possible only if the control belongs to a naming container different from the page.
In turn, ASP.NET generates the value for the UniqueID property based on the value of the ID property that the programmer indicates. If no ID has been specified, ASP.NET autogenerates a name such as _ctlX, where X is a progressive 0-based index. If the control’s naming container is the host page, UniqueID simply takes the value of ID. Otherwise, the value of ID is prefixed with the string representing the naming container and the result is assigned to UniqueID.
What if the returned markup contains multiple elements that need a client ID? The author of the control is responsible for ensuring that any required ID is available and unique. The need for multiple IDs arises when multiple individual controls are aggregated in a hierarchy. Since its first version, ASP.NET has implemented a built-in algorithm that prevents name conflicts on hierarchies of controls. As an example, think of a DataGrid control where the first cell of each column contains a text box. In your server template for the grid, you put a TextBox control with a given ID.