Building your own simple country list dropdown component

You could have visited a lot of websites and filled out many forms in which you would have entered the country you are from. Most of the applications that need registration would collect this data from the user. When you attempt to create a dropdown list it is tedious to enter all the country names and assign a value to each country.


It is definitely going to consume a lot of time if you do so. Although there are third party components available for a mere sum that you can buy and use it for lifetime, you can yourself create a simple component. It is so easy to create a drop down list component which can be used in all your applications.

Since you will be inheriting the dropdownlist control you have to Import the System.Web.UI, System.Web.UI.WebControls, and the System.ComponentModel namespaces using the imports statements above the component class.

As a next step you have to create a class called DDLCountry and then add two properties namely Text and ValueLst as given in the code below:

<DefaultProperty("Text"), ToolboxData("<{0}:DDLCountry _ runat=server></{0}:DDLCountry>")> Public Class DDLCountry _
Inherits System.Web.UI.WebControls.DropDownList
Public Enum ValueListOption
CC = 1
CN = 0
End Enum
Dim _text As String
Dim vlo As ValueListOption
<Bindable(True), Category("Appearance"), DefaultValue("")> Property _ [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("0")> Property _ [ValueLst]() As ValueListOption
Get
Return vlo
End Get
Set(ByVal Value As ValueListOption)
vlo = Value
LoadList()
End Set
End Property

The code given above creates two properties. As a next step you have to create a subroutine that will load the values and the names in the dropdownlist when the control is added to the webform. Once this subroutine is created you have to call this subroutine in the Init event. This enables the control to be initialized with the values and names in the dropdown list when the control is added to the webform.

The following code snippet would add the country names and the value for each country in the drop down list.

Dim licol As New WebControls.ListItemCollection()
Dim li As ListItem

Dim nav As New ListItem()
'This just displays the text Country
nav.Text = "—Country List--"
nav.Value = ""
licol.Add(nav)

Dim af As New ListItem()
af.Text = "Afghanistan"
af.Value = "AFG"
licol.Add(af)

Dim al As New ListItem()
al.Text = "Albania"
al.Value = "ALB"
licol.Add(al)
'add other countries below...

'add listitemcollection to the list

For Each li In licol
Me.Items.Add(li)
Next

This is one way of adding the country names and values to a dropdown list and you have to compile this to create your component. Reference this component in you project and then start using it in your project. There are also some other types of web controls available for use which enables you to locate the user’s country and then display that country as the default value in the dropdown list. For this purpose you can use a database called the GeoIP database that is available from MaxMind. This database is available freely from their site which can be used for IP address lookup for each country.

Thus creating your own component is definitely a better way to proceed with your project which can reuse the country drop down component in as many web pages as you can.


_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.