If you have been using ASP.NET themes feature in your ASP.NET projects, you surely must have come across situations where you wished you could set certain properties of controls via skins. For example, in all my ASP.NET projects, I always want to set the Display property of any validator control to Dynamic. Also, I simply hate the idea of setting the ValidationExpression property for every instance of the RegularExpressionValidator control I use in aspx / ascx files where I have to validate an email address. When I tried to set these two properties of the RegularExpressionValidator control via skin, I got the following errors:
- The 'Display' property of a control type System.Web.UI.WebControls.RegularExpressionValidator cannot be applied through a control skin.
- The 'ValidationExpression' property of a control type System.Web.UI.WebControls.RegularExpressionValidator cannot be applied through a control skin.
This happens because the Display as well as the ValidationExpression properties of the RegularExpressionValidator control are not marked as Themeable. To overcome this, I found a really cool and simple solution (object inheritance to the rescue !!!). I created a new class called NewRegularExpressionValidator which inherits from the RegularExpressionValidator class, and provides a new implementation of these two properties by marking them as Themeable. I created this new class in a new class library project (named ClassLibrary1) and also added the System.Web reference to this class library project. Below is the code for the NewRegularExpressionValidator class:

Next I added a reference to ClassLibrary1 project from my ASP.NET project and defined two skins (Default skin and Email skin) for the NewRegularExpressionValidator control. Have a look at the skin definitions below:

Now I no longer have to explicitly set the Display and ValidationExpression property of each and every RegularExpressionValidator control which validates an email address. Instead, I can simply set their SkinID as Email ;-). See below screen shot on how to use the Email skin in aspx / ascx files:

This also makes it super easy for me to change the ValidationExpression property for a particular type of validation if need be. For example, if a new business rule disallows all hotmail email addresses, I can simply change the ValidationExpression property in the Email skin instead of changing it in multiple aspx / ascx files where I am validating email addresses ;-)
Also note that the Default skin makes sure that every instance of the NewRegularExpressionValidator control in my ASP.NET project (which does not specify a particular SkinID) will have its Display property automatically set to Dynamic ;-)
Note: You can use the same above trick with many other controls to make their Non Themeable properties Themeable. One good example (which I have been using since years) is to make the MaxLength property of a TextBox control Themeable. I find it quite useful to set the MaxLength property of a TextBox through a skin for common inputs which are required in multiple pages of a website (like Email, Password, etc).
Cheers,
Raj
~~~ CODING FOR ETERNITY !!! ~~~