Building your own simple country list dropdown componentYou 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 _ 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 nav As
New ListItem() Dim af As
New ListItem() Dim al As
New ListItem() 'add listitemcollection to the list For Each
li In licol 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 users 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.
|