I have worked on a variety of ASP.NET web applications over the past few years and almost all of them needed to collect address data in some form or another. One thing I have never been able to find out there is a decent ASP.NET based user control that allowed the selection of locale (state / province & country.) With this idea in mind, I set out to create my ASP.NET “Locale LINQ” user control.
What It Do
So here are the basic features:
- Displays the corresponding States/Provinces based on the selected Country
- Includes a pretty exhaustive list of Countries & their associated States/Provinces
- Allows for an initial Country or State/Province to be selected via markup or server side code
- Caches the list of States/Provinces & Countries for great performance
- Based on an XML file containing the list of States/Provinces & Countries
- Uses LINQ for all of the data access & queries
- Based on the .NET 3.5 Framework
Markup Properties
The following properties are available in the user control ASPX markup:
<uc1:ucLocale ID="ucLocale1" runat="server"
EnableProvinceAutoPostback="true"
InitialCountrySelected="United States"
InitialProvinceSelected="Hawaii"
OnSelectedCountryChanged="ucLocale1_SelectedCountryChanged"
OnSelectedProvinceChanged="ucLocale1_SelectedProvinceChanged" />
EnableProvinceAutoPostback: Sets whether or not a PostBack occurs when a State/Province is selected
InitialCountrySelected: Sets the initial Country that you would like to be selected when the control is displayed
InitialProvinceSelected: Sets the initial State/Province that you would like to be selected when the control is displayed. Setting this value will also ensure that the related Country is also displayed.
OnSelectedCountryChanged: Event handler for the control's OnSelectedCountryChanged event
OnSelectedProvinceChanged: Event handler for the control's OnSelectedProvinceChanged event
Data & Caching
All of the Province/State & Country information is in the App_Data\CountriesAndProvinces.xml file. I use LINQ to XML to load all of the data from the file and then cache the data in memory after the initial load. Caching the data places it in the server's memory, which will greatly improve performance as a read from the data file will only take place when the cache expires.
// Cache timeout length
public static DateTime CacheTimeout
{
get
{
return DateTime.Now.AddHours(24);
}
}
Download
You can download the control & full source here:
http://aiecpg.blu.livefilestore.com/y1p0t40ilR8hsdGGS8U-2uafnrWQ7S3q99apBcJ7aY5vYSal9vKeoaU7LVOx-6hbK7SB9c4suXmCH0/LocaleLinq.zip?download