Monday, June 30, 2008

ASP.NET Locale (State/Province, Country) LINQ User Control

Well, its official - I love LINQ. I don't know why but when I say that I think of Steve Correl in the beginning of Anchorman standing there putting mayonnaise in the toaster and saying... "I like to eat ice cream and I really enjoy a nice pair of slacks." Anyways, back to LINQ...

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:

(DO NOT redistribute without my express permission or take credit for my work please.)

I hope that you find this control useful... Please leave comments about what you think about the control & links to sites you use it in.

- Aaron
http://www.churchofficeonline.com


kick it on DotNetKicks.com

4 comments:

javarules said...

How old is this? I note that the US States do not include Puerto Rico or the U.S. Virgin Islands.

Anonymous said...

Great control,thank you :)

harvey007 said...

Great control!

Anonymous said...

порно онлайн малолетке девушки http://free-3x.com/ молоденькие онлайн видео free-3x.com/ порно фото американских студентов [url=http://free-3x.com/]free-3x.com[/url]