Get All Countries List in Asp.net using System.Globalization with C#

Introduction:
This article explains how to bind all countries to dropdownlist in asp.net using c#. By using System.Globalization namespace we can get all countries list in asp.net using c#.

Aspx Page:

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>binding all countries to dropdownlist in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Select Country:</b><asp:DropDownList ID="ddlCountries" runat="server"/>
</div>
</form>
</body>
</html>

C# Code:

using System;
using System.Web;
using System.Globalization;
using System.Collections.Generic;
using System.Web.UI.WebControls;

 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> objcountries = new List<string>();
CultureInfo[] objculture = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getculture in objculture)
{
RegionInfo objregion = new RegionInfo(getculture.LCID);
if (!(objcountries.Contains(objregion.EnglishName)))
{
objcountries.Add(objregion.EnglishName);
}
}
objcountries.Sort();
ddlCountries.DataSource = objcountries;
ddlCountries.DataBind();
}
}

OutPut:
Get All Countries List  in Asp.net

Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment