.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind Countries to a DropDownList in ASP.NET

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

How to get list of all countries and bind to a dropdown list in ASP.NET.
 


How to get list of all countries and bind to a dropdown list in ASP.NET

Create an ASP.NET application and add a DropDownList to the page something like this.

<asp:DropDownList ID="ddlCountry" runat="server">asp:DropDownList>

 

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

 

public List<string> GetCountryList()

    {

      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;

    }

 

ddlLocation.DataSource = GetCountryList();

        ddlLocation.DataBind();

ddlLocation.Items.Insert(0, "Select");

Note: Updated attachement contains code for orderby the list using LINQ.




 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend