Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Load all the Country Names of the World in DropDown

, 29 Mar 2011
Rate this:
Please Sign up or sign in to vote.
DescriptionT...

Description

This shows how to bind Country names in DropDownList & also it contains some advantages.

Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head   runat="server">
    <title>Bind Country names in Dropdownlist</title>
</head>
<body>
    <form id="form1"   runat="server">
    <div>
        <asp:DropDownList ID="ddlLocation" runat="server">        
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Collections;
using System.Text;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ddlLocation.DataSource = CountryList();
        ddlLocation.DataTextField = "Key";
        ddlLocation.DataValueField = "Value";
        ddlLocation.DataBind();
    }
 
    public SortedList CountryList()
    {
        SortedList slCountry = new SortedList();
        string Key = "";
        string Value = "";
 
        foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!slCountry.Contains(info2.EnglishName))
            {
                Value = info2.TwoLetterISORegionName;
                Key = info2.EnglishName;
                slCountry.Add(Key, Value);
            }
        }
        return slCountry;
    }
}

Advantages

  • Here, Country names are loading from code behind so we can apply some filter/restriction for the items list here.(Example: Loading European countries, Loading Country names starts with 'A', etc.,)
  • Here TwoLetterISORegionName is used to DataValueField for DropDownList but we can use other things such as ThreeLetterISORegionName, NativeName, ISOCurrencySymbol, GeoId, CurrencySymbol, etc., from RegionInfo class
  • We can sort the Key field because SortedList was used here

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

thatraja
Web Developer
India India
Programmer | Dude | ||||||
 
About him & his blog? check out here. Currently he's updating the site, he'll return soon there with more things*.
 
In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon.
 
Favorite techs are MVC, WCF, Mobile, HTML5, CSS3, jQuery, etc.,
 
His hobbies are watching movies, CP, being alone, etc., But now he's working on something*.
 
* - SCAMP???
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 5 best solution so far Pingroupjanwel26-May-11 21:12 
GeneralReason for my vote of 5 good PinmemberCS201130-Mar-11 5:32 
GeneralReason for my vote of 5 This probably would be the best solu... PinmemberDrABELL29-Mar-11 9:05 
GeneralRe: Yes it was me who reported that, a few countries were missin... PinmemberTarun.K.S1-Apr-11 7:50 
GeneralBut not all countries are getting displayed. PinmemberTarun.K.S29-Mar-11 4:03 
GeneralRe: I just noticed that...I'll update the code Tarun. Thanks. Pinmvpthatraja29-Mar-11 17:53 
GeneralRe: But not all countries are getting displayed. PinmemberMember 966201012-Dec-12 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 29 Mar 2011
Article Copyright 2011 by thatraja
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid