How To Bind Countries Into Dropdownlist From CultureInfo Class

Consider that you have the following dropdown list declared on an ASP.NET page: <asp: DropDownList id=“ddlCountry” runat=“server”/>

Then from code-behind, call this method which binds the countries alphabetically to the dropdown:

using System;
using System.Data;
using System.Configuration;
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 System.Collections.Specialized;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)


    public void BindCountries()
        StringDictionary dic = new StringDictionary();
        System.Collections.ArrayList col = new System.Collections.ArrayList();
        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures
        & ~CultureTypes.NeutralCultures))
            RegionInfo ri = new RegionInfo(ci.LCID);
            if (!dic.ContainsKey(ri.EnglishName))
                dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant());

            if (!col.Contains(ri.EnglishName))


        ddlCountry.Items.Add(new ListItem("[Not specified]", ""));
        foreach (string key in col)
            ddlCountry.Items.Add(new ListItem(key, dic[key]));

        if (ddlCountry.SelectedIndex == 0 && Request.UserLanguages != null
        && Request.UserLanguages[0].Length == 5)
            ddlCountry.SelectedValue = Request.UserLanguages[0].Substring(3);

The method first adds all the countries from the CultureInfo class to a dictionary and then sorts it alphabetically. Last, it tries to retrieve the country of the browser so it can auto-select the visitors country. There might be a prettier way to sort a dictionary, but this one works.

Next Post
No Comment
Add Comment
comment url