O'Reilly logo

ADO.NET Cookbook by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.5. Localizing Client-Side Data in a Web Forms Application

Problem

You need to format dates and currency values according to the culture of the client rather than the server.

Solution

Use client culture and encoding to return data to the client formatted according to the client’s localization settings rather than the server’s settings.

The sample code-behind for the Web Forms page contains one event handler and a single method:

Form.Load

Creates the CultureInformation object based on the user’s settings.

RefreshData( )

This method sets the CurrentCulture for the current thread and demonstrates the effect on output of different data types.

The C# code for the code-behind is shown in Example 3-5.

Example 3-5. File: ADOCookbookCS0305.aspx.cs

// Namespaces, variables, and constants using System; using System.Threading; using System.Globalization; using System.Data; using System.Data.SqlClient; // This value would normally be retrieved from a user profile. private String DEFAULTUSERCULTURE = "en-US"; private CultureInfo ci; // . . . private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) ci = new CultureInfo(DEFAULTUSERCULTURE); else { // Create the CultureInfo object as specified by the user. if(enUsRadioButton.Checked) ci = new CultureInfo("en-US"); else if(enCaRadioButton.Checked) ci = new CultureInfo("en-CA"); else if(jaJpRadioButton.Checked) ci = new CultureInfo("ja-JP"); else if(frFrRadioButton.Checked) ci = new CultureInfo("fr-FR"); } RefreshData( ); } private void ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required