Asp.net 2.0 create a personalized Web Page

Source: Internet
Author: User
Tags case statement
Posting time: 11:47:01
Article Source: aspcool. com
Author: Liao Yunxiao Compilation
Edit responsibility:

In Web applications, we often need to create personalized Web pages. What is a personalized webpage? For example, Google, a familiar search engine, is often used. When we set the personalized language preferences for each use, for example, after using Chinese, when we use Google next time, google will display a Chinese interface page, which is very convenient. In Asp.net 2.0, we can use the profile function to construct personalized Web Pages like Google mentioned above. Next, we will give an example to illustrate that in this example, we create a calendar, each time you can choose your preferred language (Chinese, English) settings, when the user selects the language settings, the next time the user re-visits the page, the corresponding language display page will appear.

First, we use Visual Studio express Beta 1 to create a new web site, use the VB.net language, and then add the following controls to the form, such:

The following controls are added: A label Label, a calendar control, a dropdownlist control, and a button control. The dropdownlist control is set as follows:

· Text value objective
· English en-US display webpages in English
· Chinese ZH-CN display webpages in Chinese
· Auto select Auto automatically selects the language displayed on the page according to the language settings in IE browser

Next, we can start to set the resource file. Because our application is a page displayed in multiple languages, we need to set the corresponding English and Chinese resource files separately. In Visual Studio 2005, select generate local resource from the tool menu, as shown in,

When a resource file is generated, you will find that in solution manager, there is a new directory folder named localresources under the current project directory. The resource file default. aspx. resx is the resource file used by the homepage file default. aspx (default. aspx is displayed in English ). Next, right-click and select default. aspx. resx file. In the pop-up menu, select copy and paste under the localresources directory. In this way, a new resource file is copied and renamed as default. aspx. zh-CN.resx, this will be used as a Chinese resource file.

Next, edit the resource file. For example, open the resource file default. aspx. zh-CN.resx, then you will see the resource file, We can edit the Chinese resource file.

In Asp.net 2.0, you only need to add two new attributes "culture" and "uicultrue" to the @ page of The aspx header of the page, then the Web Application Program The page of the corresponding language is automatically displayed according to the preset resource file at runtime, as shown in the following section:

<% @ Page Language = "VB"
Culture = "Auto" uiculture = "Auto"
Autoeventwireup = "false" compilewith = "default. aspx. VB"
Classname = "default_aspx" meta: resourcekey = "pageresource1" %>

To test the program, open IE, set the language to Chinese, and run the program. The program automatically calls the Chinese resource file. The displayed page language is Chinese, it is not the default English (because our default. aspx is used to call an English resource file), for example:

In the above program, we only use the features of resource files. Below, we will make the program "remember" that every time a user chooses a language, like Google, remember that after each user's selection, the next time the user browses the page, the page in this language will be used.
To implement such a feature, we must use the profile feature provided in Asp.net 2.0. Profile can use the database to store personalized information about users, which is a bit like a session object. However, the session object has a lifetime, and the session object automatically becomes invalid after the lifetime. The profile is different unless it is explicitly removed. To implement the profile function, you must first define it in Web. config as follows:

<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 the profile attribute, you can:

Profile. Language = "En-us"
Profile. info. lastmodified = now
Profile. info. dateselected = calendar1.selecteddate

In web. congfig, some attributes/values are defined to store the variables and values to be saved, such as the lastmodified attribute, and their values are of the datatime type. The <group> label puts together variable values of the same or similar functions. In Beta 1, the profile uses the Access database to save these values.

To enable the user to select the language to use in the drop-down box and save it, write the following in the submit button:Code :

Sub btnset_click (byval sender as object ,_
Byval e as system. eventargs)
Profile. Language = ddllanguage. selecteditem. Value
End sub

To record the date in the user's selected calendar each time, we use profile.info. dateselected attribute to record and use profile. info. lastmodified records the time when the user selects a date in the calendar each time. The code below is as follows:

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, we can retrieve the values pre-stored in the profile object, and then display the date displayed by the last user in the calendar control, and the time when the user selected the date last time.

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

The following code must be written in the page_preinit () event to display the webpage correctly according to the language saved in the profile object during each page loading:

Dim Lang as system. Globalization. cultureinfo

'Read the language value selected by the user in the drop-down box.
Dim selectedlang as string = request ("ddllanguage ")

'If you select Auto, the auto option is displayed in the drop-down box.
If selectedlang = "Auto" then
Ddllanguage. selectedindex = 2
Exit sub
End if

'If it is PostBack, the original saved language information will be read.
If selectedlang isnot nothing then
Lang = new system. Globalization. cultureinfo (selectedlang)
Else
'If the page is called for the first time
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

System. Threading. thread. currentthread. currentculture = Lang
System. Threading. thread. currentthread. currentuiculture = Lang

Lblwelcomemessage. Text = resources. Resource. welcomemsg. tostring
Page. Title = resources. Resource. pagetitle. tostring
Lblselectlanguage. Text = resources. Resource. selectlanguage. tostring

The code is explained below. First, declare a variable of the cultureinfo type, which will save the information after the user selects the language each time.

Next, first check whether the page is PostBack. If PostBack occurs (after the page is refreshed or the control is clicked to activate the page event, the user may select another language ), then, use the language selected in the drop-down box to initialize the cultureinfo

Lang = new system. Globalization. cultureinfo (selectedlang)

If the page is called for the first time, it reads the language information of the original profile object and automatically sets the corresponding display in the drop-down box (use the case statement) based on the language of the profile object ).

Finally, we set the localization language of the system thread to set Lang, and set the value of each control to the value in the resource file (called in the form of resources. Resource. xxxx ). After the program runs, When you select English, as shown in:

The program runs as shown in figure:

In fact, the reason why Asp.net 2.0 can implement the above functions is actually recorded by an Access database called aspnetdb under the Data Directory of the project, and the aspnet_profile table is opened, you will find that the information you submit each time is recorded.

Summary:

This article describes how to use the profile feature of Asp.net 2.0 to customize web pages in local languages. The profile function uses a database to record the local linguistic information submitted by the user each time, so that the user can easily read the information to personalize the web page. We look forward to further enhancing the profile function in Visual Studio 2005 official version.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.