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 ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.