Wednesday, 3 July 2013

Country List Dropdown without DataBase in Asp.net C#

By using the Globalization class of C# you can easily do it without any database or without using lots of server resources.So let us start from basics.


What is Globalization class ?

According to MSDN, Globalization is the process of designing and developing a software product that functions in multiple cultures/locales. 

This process involves:
Identifying the culture/locale that must be supported Designing features which support those cultures/locales.
Writing code that functions equally well in any of the supported cultures/locales.
You can use the Globalization class by using the Namespace
using System.Globalization;
For more details about Globalization class you can refer the MSDN site.

I hope you understand some basic about the  Globalization class.

Now let us start to create a ASP.Net Project:

Use the following source code in the default.aspx <content> section page as:

<div class="div" align="center">
    <br /> <br />
        <asp:DropDownList ID="DropDownList1" runat="server" Width="100px">
        </asp:DropDownList>
    </div>

 Then switch to view code and Create the following method in the default.aspx.cs to get country list using Globalization:

        public static List<string> CountryList()
        {
            //Creating list
            List<string> CultureList = new List<string>();

            //getting  the specific  CultureInfo from CultureInfo class
       CultureInfo[] getCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

            foreach (CultureInfo getCulture in getCultureInfo)
            {
                //creating the object of RegionInfo class
                RegionInfo GetRegionInfo = new RegionInfo(getCulture.LCID);
                //adding each county Name into the arraylist
                if (!(CultureList.Contains(GetRegionInfo.EnglishName)))
                {
                    CultureList.Add(GetRegionInfo.EnglishName);
                }
            }
            //sorting array by using sort method to get countries in order
            CultureList.Sort();
            //returning country list
            return CultureList;
          }

Now call the above method on page load with dropdonlist to bind the country list as

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataSource = CountryList();
                DropDownList1.DataBind();
           
            }
        }
 In the above code first I have ensure that my page is not post back by using the IsPostBack property so dropdownlist can not be refreshed every page post back and after that I have just assigned the country list method to the dropdownlist Data Source property and finally calling Data Bind method to bind dropdownlist  also you can bind the above list method to any control which supports Data Source property.

Now run the application and select a Country from the DropDownList which looks as in the following image:


From the above example ,its clear that if we undersatnd .Net Framework deeply you can reduce our application code more than or atleast  50 percent which results application performance faster.

Note:
For detailed code please download the zip file attached Download this Article.

Summary
From all the examples above we see how to reduce the code required to perform these tasks. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me


2 comments:

  1. K then How to do this for states also.....

    ReplyDelete
  2. how to get states while selecting countries in dropdown list

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)