How to get automatic all country names in drop down list in asp.net c#
Many times we want to save all country names in drop
down but if we save all values it’s very difficult to save and also long time
taking process. In the below article, we have use globalization to remove this
type of problem and we can save or get the all country names in drop down list
Source Code:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" Width="250px">
</asp:DropDownList>
</form>
</body>
</html>
Code Behind(C#):-
using
System;
using
System.Collections.Generic;
using
System.Globalization;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public partial class dropdownlist_country : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.DataSource = country_name_list();
DropDownList1.DataBind();
}
}
public static List<string> country_name_list()
{
List<string> Culturelist = new List<string>();
CultureInfo[] getcultureinfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach(CultureInfo getculture in getcultureinfo)
{
RegionInfo getregininfo = new RegionInfo(getculture.LCID);
if(!(Culturelist.Contains(getregininfo.EnglishName)))
{
Culturelist.Add(getregininfo.EnglishName);
}
}
Culturelist.Sort();
return Culturelist;
}
}
Out-Put:-
How to get automatic all country names in drop down list in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
6:16:00 PM
Rating:
No comments: