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:
Then switch to view code and Create the following method in the default.aspx.cs to get country list using Globalization:
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.
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>
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();
}
}
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
K then How to do this for states also.....
ReplyDeletehow to get states while selecting countries in dropdown list
ReplyDelete