in

code for eternity !!!

community website for .net freaks ;-)

Technology

March 2008 - Posts

  • Tip/Trick: Making Non Themeable Properties of ASP.NET Controls Themeable

    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 !!! ~~~

  • Cool TrimText Extension Method for TextBox

    Extension Methods are one of the most powerful features of .NET 3.5 and they can add a lot of flexibility to your code and help in cutting down your code size significantly if used wisely. I have been using a lot of Extension Methods in my code lately. One of them is the TrimText Extension Method for TextBox. While taking user input on any webform or winform (for example when users enter their First Name / Last Name), often, users accidentally add an extra space in the end. If you are a good coder, you would handle such common user mistakes by making sure to trim these input values before adding them to the database. Prior to .NET 3.5, we would have to write the below code to trim the TextBox text:

    C#:

    TextBox1.Text = TextBox1.Text.Trim();

    VB:

    TextBox1.Text = TextBox1.Text.Trim()

    Now lets see how Extension Methods can help cut down the size of our above code. We create a simple TrimText Extension Method in a static class (C#) or module (VB) like below:

    C#:

    public static class ExtensionMethods
    {

        public static void TrimText(this TextBox t)
        {
            t.Text = t.Text.Trim();
        }

    } 

    VB:

    Imports System.Runtime.CompilerServices

    Public Module ExtensionMethods

        <Extension()> _
        Public Sub TrimText(ByVal t As TextBox)
            t.Text = t.Text.Trim()
        End Sub

    End Module 

    Once we have our TrimText Extension Method in place, we can achieve the same task by simply calling the TrimText Extension Method from any TextBox object:

    C#:

    TextBox1.TrimText();

    VB:

    TextBox1.TrimText()

    Notice how we have cut down the code and the code looks so much neater now ;-) I will post a few more examples of useful Extension Methods which I am using over the coming weeks. For those new to .NET 3.5, click here to know more about Extension Methods.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~



StopGlobalWarming.org  
Powered by Community Server (Non-Commercial Edition), by Telligent Systems