Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Be a Purveyor of Personalizable Web Pages Using ASP.NET's Profile Service : Page 2

Learn how to create ASP.NET Web pages that please visitors by displaying data in their preferred language or by remembering user-entered values from one visit to the next.




Application Security Testing: An Integral Part of DevOps

Persisting User's Preferences
While the automatic localization feature in ASP.NET 2.0 is useful, it would be much more useful if you could let the user choose the desired language while running the application instead of setting it in IE ahead of time. So I am now going to enhance my application to let users choose their preferred language using a drop-down list. In essence, the user can either set the language preference in IE, or he can select the language via the drop-down list control on the page.

Unfortunately, the Implicit Localization method discussed earlier only works when the language preference is set in IE. To let users choose their preferred language without setting the language preference in IE, you need to use Explicit Localization.

To use Explicit Localization, first create a new special folder known as Resources in Solution Explorer. Then add two resource files under the Resources folder (right-click on the Resources folder and select Add New Item…. Select Assembly Resource File) and then populate it with the content shown in Figure 7.

Figure 5. Picking China: Select Chinese from your preferences in IE to test the localization capabilities you've built.
To remember a user's language preference, I will use the new Profile service in ASP.NET 2.0. Think of the Profile service as a mechanism to persistently store a user's information, somewhat similar to the Session object. However, a session object is only valid throughout the entire duration of the session; after the session has expired the Session object is no longer available. The Profile service, however, retains its information for as long as you want, until you explicitly remove it from the data store.

To see how the Profile service work, let's add the following Profile properties definitions into Web.config:

<system.web> <profile> <properties> <add name="language" type="string"/> <group name="Info"> <add name="DateSelected" type="System.DateTime"/> <add name="LastModified" type="System.DateTime"/> </group> </properties> </profile> ...

To use these Profile properties, you can simply reference them like this:

Profile.language = "en-US" Profile.Info.LastModified = Now Profile.Info.DateSelected = Calendar1.SelectedDate

Note that the <group> element in Web.config allows you to group similar Profile properties into a single name, in this case, Info. In Visual Studio 2005 Beta 1, the default Profile Provider uses an Access database to store the information stored by the Profile object. We will examine the database later in this article.
Figure 6. Lost in Translation: The Web application can now translate between two different languages.

But for right now I just want to use the Profile object to store the user's language preference. Once the user has selected a language in the drop-down list box and clicked the Set button, I can save the language preference in the "language" property.

Sub btnSet_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Profile.language = ddlLanguage.SelectedItem.Value End Sub

Also, I can save the date that the user has selected in the Calendar control using the Info.DateSelected property (the use of the Calendar control in this example illustrates how to persist the date data type in the profile property). The Info.LastModified property allows me to express the time and date that the property was last modified:

Sub Calendar1_SelectionChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Profile.Info.DateSelected = Calendar1.SelectedDate Profile.Info.LastModified = Now End Sub

When the page is loaded, I can retrieve the values saved in the Profile object and then set the Calendar control to the date previously saved.

Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Calendar1.SelectedDate = Profile.Info.DateSelected Response.Write("Date set on " & _ Profile.Info.LastModified) End Sub

To display the page in the language chosen by the user, I have to handle the setting of the current page culture in the Page_PreInit() event (see Listing 1). I'll walk through the code in this event so you'll have a better idea of how it works.

First, you declare a variable of type CultureInfo. This variable will hold culture information based on the language selected by the user.

Dim lang As System.Globalization.CultureInfo

The first step is to check if this is a postback. A postback can occur when the user refreshes a page or when the user clicks on one of the controls in the page. If this is a postback, I will check the language selected by the user (through the drop-down list). If the selected language is "Auto" (users can choose "Auto" if they want the language setting to always match the preference set in IE) I do not need to proceed further; the ASP.NET runtime will perform the localization:

'===see if this is a postback=== '---which language did the user select... Dim selectedLang As String = _ Request("ddlLanguage") '---if language is Auto then let the page decide ' automatically If selectedLang = "Auto" Then ddlLanguage.SelectedIndex = 2 Exit Sub End If

If the language selected is not Auto, I will load the culture information from the language drop-down control using the culture code (such as "en-US" and "zh-CN"):

If selectedLang IsNot Nothing Then '---a postback lang = New _ System.Globalization.CultureInfo(selectedLang) Else

Figure 7. Filling Out: Here are partial contents for the two resource files.
However, if the page is loading for the first time, I'll check the Profile object to see if the user has previously saved the language preference. If so, I will use the saved language preference to obtain the culture information, which the user can then immediately change using the language drop-down. As usual, if the language is set to "Auto," there is no need to process any further.

'====page is loading for the first time==== '---retreive language preference from profile ' obj If Profile.language <> "Auto" Then lang = New _ System.Globalization.CultureInfo( _ Profile.language) Select Case Profile.language Case "en-US" : _ ddlLanguage.SelectedIndex = 0 Case "zh-CN" : _ ddlLanguage.SelectedIndex = 1 Case "auto" : _ ddlLanguage.SelectedIndex = 2 End Select Else '---if language is auto, then exit ddlLanguage.SelectedIndex = 2 Exit Sub End If End If

With the culture information obtained, I can now set the CurrentCulture and CurrentUICulture properties of the current thread to the new language:

'---set the current thread to the selected ' culture System.Threading.Thread.CurrentThread._ CurrentCulture = lang System.Threading.Thread.CurrentThread._ CurrentUICulture = lang

Finally, change the display texts in controls (such as the Label controls) using the localized string (through the Resources class):

'---display localized strings lblWelcomeMessage.Text = _ Resources.Resource.WelcomeMsg.ToString Page.Title = _ Resources.Resource.PageTitle.ToString lblSelectLanguage.Text = _ Resources.Resource.SelectLanguage.ToString

Unlike with implicit localization, we are now explicitly retrieving strings to display from the resource files. This is known as Explicit Localization.

You can now either allow the ASP.NET runtime to automatically localize the Web page for you, or you can use the Profile object to let users select (and persist) their language preference.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date