Typed Styles for Child Controls Example
This example shows how to create a control named StyledRegister that implements strongly typed style properties in a composite control. These properties enable a page developer to customize the appearance of the composite control's child controls. Typed styles also allow composite controls to scale easily. As more child controls are added to the composite control, it becomes unmanageable to expose each child control's many properties in the composite control. Instead, you can encapsulate many properties in a single style property.
The StyledRegister control in the example is similar to the Register control described in Composite Web Control Example. The StyledRegister control has two TextBox child controls and a Button child control. The StyledRegister class exposes the ButtonStyle and TextBoxStyle properties to enable a page developer to set the Font, ForeColor, and other style-related properties of the child TextBox and Button controls.
A page developer can set the Font-Names, ForeColor, and BackColor properties of the controls in the StyledRegister control as shown in the following example:
<aspSample:StyledRegister ID="StyledRegister1" runat="server">
<TextBoxStyle Font-Names="Arial" BorderStyle="Solid"
ForeColor="#804000"></TextBoxStyle>
<ButtonStyle Font-Names="Arial" BorderStyle="Outset"
BackColor="Silver"></ButtonStyle>
</aspSample:StyledRegister>
As the preceding example shows, typed style properties are persisted as child elements within a control's tags. For information about implementing properties that are persisted this way, see the implementation of the Author property in Server Control Properties Example.
A typed style is a property of type Style or of a type derived from Style. The Style class exposes appearance-related properties such as Font, ForeColor, and BackColor. The ControlStyle property of the WebControl class is of type Style. The Font, ForeColor, and BackColor properties of a Web control are subproperties of the ControlStyle property, although they are also exposed as top-level properties of the WebControl class.
Because the Style class has subproperties, properties of type Style require custom state management, as described in Custom Property State Management Example. The implementation details for typed styles are described in the "Code Discussion" section later in this topic.
The StyledRegister control demonstrates the following main steps in implementing typed styles for child controls:
-
Defining properties of type Style or of types derived from Style.
-
Implementing state management for the style properties.
-
Applying styles to child controls.
Because the Style type implements the IStateManager interface, the pattern for defining a property of type Style is identical to that described in Custom Property State Management Example. You define a typed style property as a read-only property stored in a private field. In the property's accessor, you create a new Style instance only if the field corresponding to the property is null (Nothing in Visual Basic). If the control has started state tracking, you call the TrackViewState method of the newly created Style instance. The ButtonStyle and TextBoxStyle properties of StyledRegister are defined using this technique.
Similarly, the implementation of the state-management methods in StyledRegister is identical to that described in Custom Property State Management Example. In the overridden TrackViewState method of your control, you call the TrackViewState method on the base class and the TrackViewState method on each style property. In the overridden SaveViewState method of your control, you call SaveViewState on the base class and SaveViewState on each style property. The state you return from SaveViewState represents the combined state of the base class and the style properties. For ease of retrieval in the LoadViewState method, the StyledRegister control uses a Triplet object to save the combined state. The LoadViewState method performs the reverse of the SaveViewState method and loads state into the base class and into the typed styles on postback.
The last step in implementing a typed style is to apply styles to child controls. The StyledRegister control performs this step in the render phase, after view state is saved, so that state corresponding to typed styles is not saved in the view state of the child controls. If typed styles were applied to child controls when state tracking was on, these styles would be saved in the view state of the child controls. This would be inefficient, since typed style properties manage their own state. The following code excerpt from the Render method of the StyledRegister control shows the implementation pattern for applying styles in the render phase:
The following example shows an .aspx page that uses the StyledRegister control. In the Submit event handler, you would add code to enter the registration data into a database or write a cookie on the user's computer. In a production application, you would also need to check for script injection attacks. For more information, see Script Exploits Overview.
For information about compiling and using the custom control examples, see Building the Custom Server Control Examples.