Country dropdownlist control
CodeKeep C# Feed Novembre 19th, 2007
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 listLink: http://www.codekeep.net/snippets/ff5770dd-6a54-4142-afe9-7bd57f571dc7.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Win32;
namespace DynamicWebSite.Framework
{
public class CountryDropDownList : DropDownList
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//would need some kind of state persistence / one time loading here like if(!Page.IsPostBack) or what have you
this.DataSource = GetCountries();
this.DataBind ();
}
private List<string> GetCountries()
{
List<string> returnValues = new List<string>();
RegistryKey countries =
Registry.LocalMachine.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion" +
"\\Telephony\\Country List");
if (countries != null)
{
// Get the list of subkeys,
// which is actually the country code
string[] subkeys = countries.GetSubKeyNames();
foreach (string ccode in subkeys)
{
// Open the subkey, to get the country name
RegistryKey country = countries.OpenSubKey(ccode);
// ..get the country name from country key
returnValues.Add(country.GetValue("Name").ToString());
// We're done using the key, we should close it
country.Close();
}
// It's now safe to close the country list key
countries.Close();
}
returnValues.Sort();
return returnValues;
}
}
}