Creating a Custom DropDown Control

Creating a Custom DropDown Control

When working with a true OO (Object Oriented) language framework such as .NET it’s sometimes difficult to know exactly what is available to you as a developer. Some developers who come from C++ or Java backgrounds have a strong grasp of OO concepts, what they mean, and most importantly how to use them in everyday situations. A lot of developers are familiar with OO concepts but fail to grasp precisely how they can use them in real world development scenarios.

In this tutorial we will look at one of the more widely known OO concepts called inheritance. Instead of reviewing the meaning of the concept we will put in into direct action, in a visible demonstration to literally watch inheritance in action on a web page and design-time environment with Visual Studio.NET.

To demonstrate the concept we will build our own custom DropDown web component. We will see how we can derive from the existing ASP.NET DropDownList component, and how we can extend the component to include our own customized functionality.

Additionally, we will see how we can interact with our custom DropDown component in the visual designer of Visual Studio.NET to update and reflect changes made to our custom component at design-time. By being able to watch our custom component visually on a Web Form the concept of inheritance becomes much clearer as we can literally see the inheritance in action in real time.

Note: The custom DropDownList component will be implemented in both VB.NET as C#

$2.79
- OR -

Overview

Web Control Libraries

Is everything we described above really possible? Absolutely, in fact many developers aren’t even aware this is possible with the .NET framework or simply forget they have this feature at their disposal. Web control libraries are probably one of the greatest advancements made in Web development through ASP.NET.

A web control library is a type of project available in Visual Studio.NET. As a project, it is simply compiled into a .NET assembly and will contain one or more custom web controls. These web controls can then be used on your own ASP.NET Web Forms. In fact, since the library is a compiled assembly, you can add your own custom controls to the Toolbox in Visual Studio.NET and use drag-drop features just like any other web control.

This is all possible through the use of inheritance. To grasp the concept you have to think in terms of classes and code, instead of visual components on a web page. Every ASP.NET control is nothing more than a class defined in the .NET framework. As such, you are free to inherit from these classes to suit your own needs.

Your custom web control therefore, is simply a class definition which inherits from an existing control in the .NET framework. In fact you can even create composite controls which are aggregates of multiple controls. You could theoretically create a single component which consisted of a Label, 2 Textboxes and a submit button. The possibilities are literally endless.

The trick to creating custom controls which will still be rendered correctly in the visual designer for Visual Studio.NET is to derive from a single explicit control. Explicit meaning non-generic because by default your control will inherit from the generic class “WebControl”, which has no real meaning in terms of a web page. So you want to try and choose a specific single control if possible, such as a Textbox, Button, Label etc…

Kevin Koch

Kevin KochKevin Koch is a senior software engineer with over 8 years experience designing and architecting primarily web based applications. Fresh out of college during the nineties he co-founded Task Solutions and developed several projects with the then popular classic ASP.

During the Dot Com boom Kevin left his position as president and joined a new venture to build an enterprise insurance claim system build upon J2EE technology. After the Dot Com crash Kevin schooled himself to become an expert with .NET technology and is currently freelancing his ASP.NET skills to build enterprise n-tier frameworks using advanced OO methodologies.

See All Postings From Kevin Koch >>

Reviews

Be the first to write a review

You must me logged in to write a review.