Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Article

A DropDownList for U.S. States/Canadian Provinces and Countries

Rate me:
Please Sign up or sign in to vote.
2.44/5 (16 votes)
3 Nov 20032 min read 130.6K   1.8K   45   14
This article discusses subclassing the DropDownList to create a DropDownList that is preloaded with U.S. States and Canadian Provinces

Introduction

My task was to create a simple DropDownList containing U.S. States and Canadian Provinces that could be reused easily. The requirements were that it should be entirely self-contained and should operate exactly like any other DropDownList.

For this reason, it made the most sense to subclass the DropDownList component, and load the ItemList within the overloaded OnInit event.

Contents

This package contains two controls, SDIddlStates and SDIddlCountries. This article contains code snippets from the SDIddlStates control. However, the SDIddlCountries control is almost identical, as you will see as soon as you look at it. In fact, you could easily combine the two and add a new parameter that the user could set at design-time to establish which type it is. That would be a very good exercise.

Using the code

To use the code, simply compile it. Then, within the Visual .NET IDE, choose the TOOLS | Add/Remove Toolbox Items... menu option and browse until you find the DLL that resulted from the compilation. You should them be able to drag-and-drop the control onto any Web Form you need it.

Points of Interest

Accessing the required Classes

The code requires the use of certain classes, so we must grab their definitions, like this:

C#
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

Subclassing the DropDownList

The next thing we need to do is begin the code by declaring the new class, which I call SDIddlStates, like this:

C#
public class SDIddlStates : DropDownList
{
...all of the code will go here...
}

Overriding the OnInit Event

In order to preload the ItemList, which contains the Values and Text of the DropDownList control, I wrote the following code:

C#
protected override void OnInit(EventArgs e)
{
    // Create the DataSource that contains the data
    this.DataSource = CreateDataSource();

    // Tell the control what column in the DataSource contains the Text
    this.DataTextField = "StatesTextField";

    // Tell the control what column in the DataSource contains the Value
    this.DataValueField = "StatesValueField";

    // Bind the DataSource to the control
    this.DataBind();

    // Do whatever the control usually does OnInit
    base.OnInit(e);
}

Creating the DataSource

In order to create the DataSource that is used in the code, above, I wrote the following code:

C#
protected ICollection CreateDataSource()
{

    // Create a table to store data for the DropDownList control.
    DataTable dt = new DataTable();

    // The first column of the DataSource contains the Text
    dt.Columns.Add(new DataColumn("StatesTextField", typeof(String)));

    // The second column of the DataSource contains the Value
    dt.Columns.Add(new DataColumn("StatesValueField", typeof(String)));

    // Populate the table with rows.
    dt.Rows.Add(CreateRow("0", "Choose a State/Province", dt));
    dt.Rows.Add(CreateRow("AL", "Alabama", dt));
    dt.Rows.Add(CreateRow("AK", "Alaska", dt));
    ...
    dt.Rows.Add(CreateRow("SK", "Saskatchewan", dt));
    dt.Rows.Add(CreateRow("YT", "Yukon Territories", dt));

    // Create a DataView from the DataTable to act as the
    // DataSource for the DropDownList control.
    DataView dv = new DataView(dt);
    return dv;
}

Creating the DataRow

In order to create the DataRow that is used in the code, above, I wrote the following code:

C#
protected DataRow CreateRow(String Value, String Text, DataTable dt)
{
    // Create a DataRow using the DataTable defined in the
    // CreateDataSource method.
    DataRow dr = dt.NewRow();

    // The first column of the DataSource contain the Text
    dr[0] = Text;

    // The second column of the DataSource contain the Value
    dr[1] = Value;

    return dr;
}

Putting the Control in the Toolbox

To be able to drag-and-drop the control from the Toolbox, I had to make sure I could put it in the toolbox to begin with. So I wrote this code:

C#
[
ToolboxData("<{0}:SDIddlStates runat=server>")
]

Note that the name of the control in the Toolbox must match the class name.

History

V1.01

  • Removed duplicate province.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have been developing software since 1984, when I began working with Digital Equipment Corporation. In 1990, I started by own independent company, Systems by Design of Illinois.

I was trained using a language (Pascal) that was designed (by Niklaus Wirth) to teach programming concepts of data structures and algorithms. My early practical experience was for a company that demanded high-quality, documented code. We built large systems using DEC's VAX/VMS Pascal.

Because of this experience, I am particularly uncomfortable with the fact that C++, which is agruably one of the worst languages ever created, is being used to teach introductory computer classes.

I am also uncomfortable with badly written documentation, which is often confounded by the introduction of material that is extraneous to the point being explained. This makes the learning and practical use of new development tools more difficult than desired.

In the current horrendous economic climate, I devote most of my time and energy to non-profit organizations, living off the money saved from doing work in the corporate world when the business climate was more favorable.

Comments and Discussions

 
GeneralMissing Alberta AB Pin
Kyle Morris9-Aug-07 11:55
Kyle Morris9-Aug-07 11:55 
QuestionStates / Provences DDL Pin
stixoffire12-Mar-07 23:52
stixoffire12-Mar-07 23:52 
Generali want to retrieve from database Pin
LovelyHelp7-Feb-06 1:03
LovelyHelp7-Feb-06 1:03 
QuestionWhat type of project Pin
fawaleb1-Aug-04 5:57
fawaleb1-Aug-04 5:57 
Generalquite useful Pin
amkarkha28-May-04 13:12
amkarkha28-May-04 13:12 
QuestionHow to build dll Pin
skofroth27-Jan-04 12:39
skofroth27-Jan-04 12:39 
AnswerRe: How to build dll Pin
hougie402-Apr-04 5:04
hougie402-Apr-04 5:04 
GeneralRe: How to build dll Pin
atoms18-Mar-05 13:40
atoms18-Mar-05 13:40 
GeneralSystem.Data vs. System.Collection Pin
Gregory Bush10-Dec-03 8:04
Gregory Bush10-Dec-03 8:04 
QuestionDoes this Rebuild list items every time? Pin
chris212110-Nov-03 16:09
chris212110-Nov-03 16:09 
AnswerRe: Does this Rebuild list items every time? Pin
jrfinkel10-Nov-03 17:27
jrfinkel10-Nov-03 17:27 
AnswerRe: Does this Rebuild list items every time? Pin
Tyrone Davis Jr.9-Aug-07 6:41
Tyrone Davis Jr.9-Aug-07 6:41 
GeneralNunavut * 2 Pin
Warren Stevens3-Nov-03 4:57
Warren Stevens3-Nov-03 4:57 
GeneralRe: Nunavut * 2 Pin
jrfinkel5-Nov-03 7:46
jrfinkel5-Nov-03 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.