Load all the Country Names of the World in DropDown
DescriptionT...
Description
This shows how to bindCountry
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"><asp:DropDownList ID="ddlLocation" runat="server"></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 toDataValueField
forDropDownList
but we can use other things such asThreeLetterISORegionName
,NativeName
,ISOCurrencySymbol
,GeoId
,CurrencySymbol
, etc., fromRegionInfo
class - We can sort the
Key
field becauseSortedList
was used here