This is an important step to take before accessing the control hierarchy when working with
composite controls.
152 CHAPTER 4 ?– T HE WEBCONTROL BASE CLASS AND CONTROL S TYLES
LabelStyle and TextBoxStyle
Now that we have code to create the child Label and TextBox controls, as well as code to get and
set their Text properties, we can move on to demonstrating how to implement custom styles.
Both LabelStyle and TextBoxStyle rely on private instances of the Style class to hold style
properties. The private instance is exposed via a read-only property. One of the tasks for this
read-only property is to make an instance copy of the configured Style class for each child
control available. The other task of the property is to manage ViewState tracking for style settings:
Style labelStyle;
public virtual Style LabelStyle
{
get
{
if (labelStyle == null)
{
labelStyle = new Style();
if (IsTrackingViewState)
((IStateManager)labelStyle).TrackViewState();
}
return labelStyle;
}
}
Style textboxStyle;
public virtual Style TextboxStyle
{
get
{
if (textboxStyle == null)
{
textboxStyle = new Style();
if (IsTrackingViewState)
((IStateManager)textboxStyle).TrackViewState();
}
return textboxStyle;
}
}
As discussed in Chapter 3, ViewState is implemented via a StateBag collection that tracks
modifications to its collection.
Pages:
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246