Both the Style class and the Control class support ViewState,
providing access through implementing the IStateManager interface:
interface IStateManager
{
bool IsTrackingViewState() { get; }
void TrackViewState();
void LoadViewState(object state);
object SaveViewState();
}
CHAPTER 4 ?– THE WEBCONTROL BASE C LASS AND CONTROL STYLES 153
The TextBoxStyle and LabelStyle properties??™ get accessor methods call the
IsTrackingViewState() method of the InputBox control to determine if the control is tracking
ViewState changes. If it is, the code ensures that state is maintained for the style properties as well.
Customizing ViewState
The WebControl class has facilities to manage ViewState serialization for styles maintained in
WebControl??™s ControlStyle property instance of the Style class. Because we are providing our
own style management implementation, we need to customize ViewState persistence mechanisms
to include our new styling information, as shown in the following code:
override protected object SaveViewState()
{
object baseState = base.SaveViewState();
object labelStyleState = (labelStyle != null) ?
((IStateManager)labelStyle).SaveViewState() : null;
object textboxStyleState = (textboxStyle != null) ?
((IStateManager)textboxStyle).
Pages:
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247