Bind Countries to a DropDownList in ASP.NET

Today, I have provided an article showing you how to get a list of all countries and bind them to a DropDownList in ASP. NET. There are many ways in which a user can get a list of all countries. Here, we use the CultureInfo class to get a list of all countries. It's easy to implement. All you have to do is implement and hook it up to your website. First of all start Visual Studio .NET and make a new ASP.NET web site using Visual Studio 2010.

Now you have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK


Now add a new page to the website.

  • Go to the Solution Explorer
  • Right-click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK


Now drag and drop a DropDownList control from the Toolbox to the page. A DropDownList is used to show a list of the countries. Let's take a look at a practical example. The .aspx code will be as shown below.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countrywithdropdown.aspx.cs"  
  2.     Inherits="Countrywithdropdown" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
  4. <html xmlns="">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <span style="color:Black"><strong>Select Country :</strong></span>    
  12.         <asp:DropDownList ID="DropDownList1" runat="server" BackColor="Brown" ForeColor="#66FF66">  
  13.         </asp:DropDownList>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>  

Add the following Namespace

  1. using System.Globalization;

CultureInfo class

The CultureInfo class contains culture-specific information, such as the language, country/region, calendar, and cultural conventions. The CultureInfo class specifies a unique name for each culture. The CultureInfo class specifies a unique name for each culture. 

GetCultures method 

The GetCultures method retrieves a complete list of all cultures.

Now double-click on the page and write the following code to get a list of all countries and bind to a DropDownList

  1. public List<string> GetCountry()  
  2. {  
  3.     List<string> list = new List<string>();  
  4.     CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);  
  5.     foreach (CultureInfo info in cultures)  
  6.     {  
  7.         RegionInfo info2 = new RegionInfo(info.LCID);  
  8.         if (!list.Contains(info2.EnglishName))  
  9.         {  
  10.             list.Add(info2.EnglishName);  
  11.         }  
  12.     }  
  13.     return list;  
  14. }

Now call this GetCountry method on your page load that will bind and display countries in the DropDownList

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     DropDownList1.DataSource = GetCountry();  
  4.     DropDownList1.DataBind();  
  5.     DropDownList1.Items.Insert(0, "Select");  
  6. }

In code-behind write the following complete code.


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Diagnostics;  
  9. using System.Globalization;  
  10. public partial class Countrywithdropdown : System.Web.UI.Page  
  11. {  
  12.     public List<string> GetCountry()  
  13.     {  
  14.         List<string> list = new List<string>();  
  15.         CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);  
  16.         foreach (CultureInfo info in cultures)  
  17.         {  
  18.             RegionInfo info2 = new RegionInfo(info.LCID);  
  19.             if (!list.Contains(info2.EnglishName))  
  20.             {  
  21.                 list.Add(info2.EnglishName);  
  22.             }  
  23.         }  
  24.         return list;  
  25.     }  
  26.     protected void Page_Load(object sender, EventArgs e)  
  27.     {  
  28.         DropDownList1.DataSource = GetCountry();  
  29.         DropDownList1.DataBind();  
  30.         DropDownList1.Items.Insert(0, "Select");  
  31.     }  
  32. }

Now run the application and test it.


Now click on the DropDownList to show the list of all countries.


Note: You can see a demo of this article by downloading this application. 

Some Helpful Resources