Friday, January 11, 2013

Custom Controls - Overriding Properties(Asp .NET/C#)

In my previous post I walked thru the process of extending a TextBox control by adding new properties.  In this post I will show how to override an existing property.  This can be useful when additional logic is required when a value of your property changes.  One example would be adding code to cleanse the data to prevent SQL injection attacks.

This post picks up where the previous post left off, I will override the text property of the CustomTextBox control I created previously.

Open the extended control created in the previous post and add the following code.


/// <summary>
/// Text Property
/// </summary>
public virtual string Text
{
    get
    {
        string o = base.Text;
        //perform any additonal logic here
        return o;
    }
    set
    {
        string o = value;
        //perform any additonal logic here
        base.Text = o; 
    }
}

Complete Source: 

CustomControls_TextProperty.zip

No comments:

Post a Comment