August 23, 2006

In many circumstances it's appropriate to assign a FontInfo structure as part of a custom server control. This easily allow programmers to adjust the look at feel of certain areas of text but can be a little tricky to implement because FontInfo doesn't have a public constructor. One way to accomplish this is to create a CSS Style object and use it's Font property to do the dirty work. This technique would look something like this:

So is that all there is to it? Well, not exactly. Unfortunately, FontInfo is not serializable, nor does it implement IStateManager making it difficult for us to track it's state if any changes are programmatically made to the structure.

The good news is that in the code above we're using a Style object to get at the underlying FontInfo. Since Style implements IStateManager we can override the following methods to make sure that the FontInfo property is always kept in ViewState across post backs.