Posts Tagged ‘server control’

Extending A Native Web Control

July 9th, 2009

Some may not know the difference but user controls are different from server controls. User controls inherit from the UserControl class while server controls can inherit from, say, the Button, DropDownList, GridView, or, well, any Control or WebControl object. Using a server control is faster than using a user control since there is no designer UI to be compiled.

The main drawback from creating a server control versus a user control is the lack of building a rich UI in most respects. Because there isn’t a .ascx page to load existing controls onto, you don’t have this ability as easily as you do with a server control (unless you programmatically add controls to a placeholder object in a composite control).

The main difference is that your control must inherit from an existing Control or WebControl object, or one that inherits from these, such as a Button.

Here’s a quick example of a DropDownList that is pre-populated with data essential to determining sex – “Male” and “Female”. You can save this as a .CS file into your app_code directory. In this example, we are essentially extending the existing DropDownList control to add additional functionality.

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;

namespace CustomControls
{
    public class GenderList : DropDownList
    {
        public GenderList()
        {
            System.Collections.Generic.Dictionary<String, String> dctGender = new System.Collections.Generic.Dictionary<String, String>();

            dctGender.Add("MALE", "Male");
            dctGender.Add("FEMALE", "Female");
            dctGender.Add("NA", "No response");

            base.DataSource = dctGender;
            base.DataTextField = "value";
            base.DataValueField = "key";
            base.DataBind();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}

Now, to use it on your page, you’ll need to either Register the control using the <%@ Register %> declaration or add a reference in the <pages><controls> … </controls></pages> section of your web.config.

<%@ Register TagPrefix="asp" Namespace="CustomControls" %>

…or in your web.config…

<pages>
    <controls>
        ....
        <add tagPrefix="asp" namespace="CustomControls"/>
    </controls>
</pages>

Notice we did not include the assembly. This is because it was not compiled into a .dll. If it is compiled into a .dll, you must include the Assembly name.

genderlist

After that, if you don’t feel like waiting for your intellisense to update, you can simply insert the control onto your page. The only difference is, you do not need to include the src attribute, since the control will be compiled (just the TagPrefix, and Namespace).

The class above is very simple. It inherits from DropDownList, so it has all the functionality of the native DropDownList class, but binds some data in the constructor. Pretty simple, but will save a lot of time when working with forms. If this seem a bit boring, another idea could be to have a StateList control that is populated with states from an XML file. Or, perhaps you find yourself always setting default attributes in your GridViews, such as GridLines=”none” or AutoGenerateColumns=”false”. Make your own and have these defaults set for you.