In this article we will learn how to dynamically bind the DropDownList with a month name using Globalization, often there is a need for Month Names but developers often refer to a DropDownList of months bound from a database table which is very time consuming and increases the complexity of the code. To overcome this problem we can use the Globalization namespace which is available in ASP.Net.
So let us start with the basics.
What is Globalization or System.Globalization Namespace ?
System.Globalization contains classes that define culture-related information, including the language, the country or region, the calendars in use, the format patterns for dates, currency and numbers, and the sort order for strings.
Now I hope you understand the basic concepts and use of System.Globalization. Now let us proceed to develop an application which dynamically binds the DropDownList with Month names using System.Globalization step-by-step.
Start by creating a website as:
Open the source view. The source code <form> tag should be as in the following:
- Start - All Programs - Microsoft Visual Studio 2010
- File - New Website - C# - Empty website (to avoid adding a master page)
- Give the web site a name such as Bind DropDownList or whatever you wish and specify the location
- Then right-click on Solution Explorer - Add New Item - Default.aspx page
- Drag and drop one dropdownlist in the <form> Section of the Default.aspx page
Now switch to "View Code" and declare the NameSpace at the top of the Default.aspx.cs page as in:
Now create the one method named DD_Monthbind() or as you wish to bind the DropDownList use the following code in it.
Now call the DD_Monthbind() method at page load event as:
Now run the application, the output will be as in the following::
From the preceding all examples we have learned how to bind the DropDownList with month names. I hope you understand it. Download the zip file of the sample application.