How to Bind Dropdownlist dynamically with Country Names in ASP.NET

No.of Views1434
Bookmarked0 times
Downloads 
Votes0
By  Bharat   On  23 Nov 2010 02:11:27
Tag : ASP.NET , Globalization and Localization
In this article, i will show to how to get list of countries names using CultureInfo in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, i will show to how to get list of countries names using CultureInfo in ASP.NET.When you are work with online web site, you may want to get list of country names and add or bind into dropdownlist to choice by visitors or users.So I have done same things to my project, here would like to share with you.

Implementation

Let's  create a web project using visual studio and then drag and drop or add a drop down list into the page, html code will be following,

Html Code

<form id="form1" runat="server">
    <div>
      <asp:DropDownList ID="ddlLocation" runat="server"></asp:DropDownList>
    </div>
</form>

Now i'm going to add server side code to get the list of countries name.before start write code, you have to add namespace,

using System.Globalization;

Once you are add the above name space,lets write code as like following,

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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> Country = GetCountryList();
            var sortedCountry = from c in Country orderby c select c;
            ddlLocation.DataSource = sortedCountry;
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, "Select");
            sortedCountry = null; Country = null;
        }
    }

    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;
    }
}

Here i have used CultureInfo to get all region information.and then I'm adding each countries name into the string based generic list object finally returning list of counties from GetCountryList method.

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;

And then in the page load event,only first load page, i have used LINQ to select countries name in sort order and bind to drop down list .

Note:if you not familiar with LINQ, please read this articles to learn.

List<string> Country = GetCountryList();
            var sortedCountry = from c in Country orderby c select c;
            ddlLocation.DataSource = sortedCountry;
            ddlLocation.DataBind();

That's all. hopes help to all.

Conclusion

In this article, you learned to how to get list of countries names using CultureInfo class in ASP.NET.thank you for reading. please post your all comments.

 
Sign Up to vote for this article
 
About Author
 
Bharat
Occupation-Software Engineer
Company-Espranza
Member Type-Junior
Location-India
Joined date-13 Nov 2010
Home Page-
Blog Page-http://usingaspdotnet.blogspot.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top