Themes in ASP.NET 1.1

Published 12 February 04 06:34 PM | despos

As hinted at in this post, I'm trying to implement themes in ASP.NET 1.x applications. So far I've used an XML file (sort of DataSet) with prop/value pairs for each skinned control. These settings are then translated to code and added to the project. The same process can be accomplished using dynamic compilation like that of ASP.NET pages.

To the extent I can judge having looked at the ASP.NET 2.0 implementation with a couple of decompilers, this implementation is really close to that you find in ASP.NET 2.0.

The only problem left is--what's the best format for a theme file?

ASP.NET 2.0 uses a sort of markup. Basically, you can shape your control in VS.NET and then cut-and-paste the ASPX markup in the theme file. The problem is, how can I extract the settings out of this markup? Again, ASP.NET 2.0 seems to use the template parser--a class marked as internal and not usable outside system.web.

The idea of writing a parser doesn't really excite me. I'm not like my good friend Francesco Balena who really enjoys parsers of any type...

There's a method in the Page class that sounds promising. It is ParseControl. It takes a markup string and creates the corresponding control! My idea is

  • Read blocks of markup out of the theme file
  • For each block, create the corresponding control (a temporary control)
  • Parse the markup and extract all the properties explicitly set (the properties that make up the skin)
  • Read the value of these properties from the temporary control and use these values to override the corresponding property on the original control

I didn't know an effective way to accomplish the last point. Using reflection seems to be obvious but exactly how?

Chris Anderson contributed this code--which is just what I was looking for.

object o = this;
PropertyDescriptor bg = TypeDescriptor.GetProperties(o)["BackColor"];
TypeConverter c = bg.Converter;
object v = c.ConvertFromString("Red");
bg.SetValue(o, v);

 

Really cool. I was too lazy to look carefully into PropertyDescriptor!

Comments

# Paul D. Murphy said on February 12, 2004 01:33 PM:


Why don't you just serialize and deserailze style classes. That's what I do.


public class MyLabel : Label
{
public MyLabel()
{

this.ApplyStyle(Styles.NormalText);

}
}

public class Styles
{
private static Style normalTextStyle;

private Styles(){}

public static Style NormalText
{
get
{
if(normalTextStyle == null)
{
// desearize a style class here
}

return normalTextStyle;
}
}
}

Leave a Comment

(required) 
(required) 
(optional)
(required)