Skip to main content

Get country list in .net using CultureInfo

Get country list in .net using Culture Info


You have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK
img5.gif
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
img6.gif
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.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countrywithdropdown.aspx.cs"
    Inherits="Countrywithdropdown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span style="color:Black"><strong>Select Country :</strong></span>&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" BackColor="Brown" ForeColor="#66FF66">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
Add the following Namespace.
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.
public List<string> GetCountry()
    {
        List<string> list = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);
        foreach (CultureInfo info in cultures)
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!list.Contains(info2.EnglishName))
            {
                list.Add(info2.EnglishName);
            }
        }

        return list;
    }

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

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

In code-behind write the following complete code.
Code-behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Diagnostics;
using System.Globalization;

public partial class Countrywithdropdown : System.Web.UI.Page
{
    public List<string> GetCountry()
    {
        List<string> list = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures | CultureTypes.SpecificCultures);
        foreach (CultureInfo info in cultures)
        {
            RegionInfo info2 = new RegionInfo(info.LCID);
            if (!list.Contains(info2.EnglishName))
            {
                list.Add(info2.EnglishName);
            }
        }

        return list;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.DataSource = GetCountry();
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select");
    }
}

Now run the application and test it.

img1.gif

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


img2.gif

Comments

Popular posts from this blog

Asp.Net Web API

What is ASP.NET Web API? ASP.NET Web API is a framework provided by the Microsoft with which we can easily build HTTP services that can reach a broad of clients, including browsers, mobile, IoT devices, etc. ASP.NET Web API provides an ideal platform for building RESTful applications on the .NET Framework.   Difference between ASP.NET Web API and WCF Web AP I is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocol like HTTP, TCP, MSMQ, etc. It supports multiple protocols like HTTP, TCP, Named Pipes, MSMQ, etc. WCF ships out with the .NET Framework. Both Web API and WCF can be self-hosted or can be...