Here I will explain how to bind all countries in dropdownlist in asp.net using c#(System.Globalization)
Default.aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind all countries in dropdownlist in asp.net(c#)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Country:</b><asp:DropDownList ID="ddlCountry" runat="server" />
</div>
</form>
</body>
</html>
Default.aspx.cs :
add following namespaces in code behind
using System.Collections.Generic;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> objcountries = new List<string>();
CultureInfo[] objCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getcultureinfo in objCultureInfo)
{
RegionInfo objRegionInfo = new RegionInfo(getcultureinfo.LCID);
if (!(objcountries.Contains(objRegionInfo.EnglishName)))
{
objcountries.Add(objRegionInfo.EnglishName);
}
}
objcountries.Sort();
ddlCountry.DataSource = objcountries;
ddlCountry.DataBind();
}
}
Default.aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind all countries in dropdownlist in asp.net(c#)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Country:</b><asp:DropDownList ID="ddlCountry" runat="server" />
</div>
</form>
</body>
</html>
Default.aspx.cs :
add following namespaces in code behind
using System.Collections.Generic;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> objcountries = new List<string>();
CultureInfo[] objCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getcultureinfo in objCultureInfo)
{
RegionInfo objRegionInfo = new RegionInfo(getcultureinfo.LCID);
if (!(objcountries.Contains(objRegionInfo.EnglishName)))
{
objcountries.Add(objRegionInfo.EnglishName);
}
}
objcountries.Sort();
ddlCountry.DataSource = objcountries;
ddlCountry.DataBind();
}
}
Comments
Post a Comment