Localizing Client-Side Data in a Web Forms Application - Pdf 72

[ Team LiB ]Recipe 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;

// Retrieve details about the culture.
cultureNameLabel.Text = CultureInfo.CurrentCulture.Name +
" (" + Thread.CurrentThread.CurrentCulture.Name + ")";
cultureEnglishNameLabel.Text =
CultureInfo.CurrentCulture.EnglishName;
cultureNativeNameLabel.Text =
CultureInfo.CurrentCulture.NativeName;
}

// Sample data that might come from a database
// displayed according to culture set by user.
dateLabel.Text = DateTime.Now.ToString("D");
shortDateLabel.Text = DateTime.Now.ToString("d");

Double d = 12345.678;
numberLabel.Text = d.ToString( );

currencyLabel.Text = d.ToString("c");
}
Discussion
In a globalized application, a server can be processing requests for users around the
world. Culture information for each user must be stored and made available to the server
when it is processing each request from the user so that culture-specific operations are
performed properly.
There are many ways to store the culture information for a user. You can store it
persistently on the client in a cookie. Or you can store it in a database on the server and
store it to a session variable when the client logs in or on an ad-hoc basis. No matter how
the culture information is stored, it needs to be made available to the server as the client
navigates through the site. For example, you can do this using session variables, the URL,
or hidden fields. Once the server knows the culture of the user, it can use this information


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status