Display Country List Without Database in ASP.Net C#

Background

There is often a need to display a county list. For that many beginners and developers use a database to store the list of countries and they are displayed using a specific control but by using this method there are many server resources used that results in the application becoming slow and it is not be possible to display all the countries of the world exactly.

But by using the Globalization class of C# you can easily do it without any database or without using many server resources, so by considering the above requirement I have written this article for beginners, students and anyone that wants to learn. So let us start from the basics.

The 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 that support those cultures/locales.
  • Writing code that functions equally well in any of the supported cultures/locales.
You can use the Globalization class using the namespace:
  1. using System.Globalization;
For more details about the Globalization class you can refer to the MSDN site.

I hope you understand some basics about the  Globalization class.

Now let us start to create a Website as:
  • Open Visual Studio from Start -> All programs -> Microsoft Visual Studio.
  • Then go to to "File" -> "New" -> "WebSite..." then select Visual C# -> Web application.
  • Then specify the name, CountryList, or any name as you wish and the location of the project and click on the OK button. The new web site is created.
Use the following source code in the default.aspx <body> section page as: 
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div class="div" align="center">  
  4.     <br /> <br />  
  5.         <asp:DropDownList ID="DropDownList1" runat="server" Width="100px">  
  6.         </asp:DropDownList>  
  7.     </div>  
  8.     </form>  
  9. </body>
Then switch to view code and create the following method in the default.aspx.cs to get the country list using Globalization:
countrylistMethod.png

Now call the preceding method on page load with the dropdonlist to bind the country list as:
CallingMethod.png

In the code above I first ensured that my page is not posted back using the IsPostBack property so the dropdownlist is not be refreshed every page post-back and then I just assigned the country list method to the dropdownlist Data Source property and finally calling the Data Bind method to bind the dropdownlist. You can also bind the preceding list method to any control that supports the Data Source property.

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


BindDropdownlist.png


From the preceding example, it's clear that if we undersatnd the .Net Framework deeply then you can reduce our application code more than, or at least, 50 percent and that results in better application performance.
 
Note:
  • For detailed code please download the Zip file attached above.
Summary
 
From all the examples above we see how to reduce the code required to do 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.  


Similar Articles