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

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives