WebControl, CssStyleCollection and ViewState

M

Marri Suliez

Does anyone know how the Style property of the WebControl class persists
between calls as if it's added to the ViewState? CssStyleCollection is
not serializable.

The following scenario leads me to believe it is being stored in
ViewState somehow:
1. Add a TextBox and a Button to a form
2. Have the Button.Click event add value to the TextBox.Style collection
3. Add a breakpoint in the Button.Click event handler immediately before
the code in step #2 is executed
4. Run the page
5. Click the button
6. When the code hits the breakpoint, note the number of TextBox.Style
items (you'll get 4, for example)
7. Allow the code in step #2 to run
8. Check the number of TextBox.Style items again (you'll get 5)
9. Click the button again
10. Repeat step 6 and you'll get 5, leading me to believe ViewState is
being used.

Any ideas? Thanks.
 
T

Teemu Keiski

Hi,

If Style is Control's default style, created in CreateControlStyle and
accessed via ControlStyle (and typed style members, like BackColor for
example), it uses control's own ViewState collection to store the needed
values. In WebControl CreateControlStyle is implemented as follows.

protected virtual Style CreateControlStyle()
{
return new Style(base.ViewState);

}

If control exposes additional styles in addition to the ControlStyle, their
state management needs to be done customized by overriding TrackViewState,
oadViewState and SaveViewState methods of the control and manually
specifying how state is tracked, loaded and saved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top