Home
Manage Your Code
Snippet: Country dropdownlist control (C#)
Title: Country dropdownlist control Language: C#
Description: the registry contains a list of countries for telephony so you can access that and build your list that way as long as your web server OS is up to date you get the latest country list Views: 346
Author: Kevin Isom Date Added: 11/19/2007
Copy Code  
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections.Generic;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12using Microsoft.Win32;
13
14namespace DynamicWebSite.Framework
15{
16    public class CountryDropDownList : DropDownList
17    {
18        protected override void OnInit(EventArgs e)
19        {
20            base.OnInit(e);
21
22                        //would need some kind of state persistence / one time loading here like if(!Page.IsPostBack) or what have you

23                this.DataSource = GetCountries();
24                this.DataBind ();
25            
26        }
27
28
29        private List<string> GetCountries()
30        {
31            List<string> returnValues = new List<string>();
32
33            RegistryKey countries =
34             Registry.LocalMachine.OpenSubKey(
35              "Software\\Microsoft\\Windows\\CurrentVersion" +
36              "\\Telephony\\Country List");
37
38            if (countries != null)
39            {
40                // Get the list of subkeys,

41                // which is actually the country code

42                string[] subkeys = countries.GetSubKeyNames();
43
44                foreach (string ccode in subkeys)
45                {
46                    // Open the subkey, to get the country name

47                    RegistryKey country = countries.OpenSubKey(ccode);
48
49                    // ..get the country name from country key

50                    returnValues.Add(country.GetValue("Name").ToString());
51
52                    // We're done using the key, we should close it

53                    country.Close();
54                }
55
56                // It's now safe to close the country list key

57                countries.Close();               
58            }
59
60            returnValues.Sort();
61            return returnValues;
62        }
63    }
64}