asp.net| Web pages in Web applications, we often create personalized Web pages. What is a personalized Web page? For example, we often use the familiar search engine Google, when we set every time to use the personalized language preferences, such as the use of Chinese, the next time you use Google, Google will appear in the Chinese interface page, very convenient. In asp.net 2.0, we can use the Profile feature to construct personalized web pages like the Google mentioned above. Below, we give an example to illustrate, in this example, we create a calendar, each time the user can choose the language (Chinese, English) to set, when the user selected the language settings, the next time the user to visit the page, the corresponding language will appear in the page.
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, as shown in the following illustration:
The following controls are added: A label label, a Calendar control, a DropDownList control, a button control, and the settings for the DropDownList control in the following way:
· Text Value Purpose
· Chinese en-US displays web page in English
· Chinese ZH-CN Display Web page in Chinese
· Auto Select Auto Selects the language of the page display automatically according to the language settings in IE browser
Next, we can start to set up the resource file. Because our application is a multilingual display of the page, so to separate the corresponding English, Chinese resource files. In Visual Studio 2005, select the Generate local Resource (generate resource file) in the Tools menu, as shown in the following illustration,
When the resource file is generated, you will find that in Solution Manager, under the current engineering directory, there is a new directory folder called LocalResources. Among them, Default.aspx.resx this resource file, is Default.aspx this homepage file to use the resource file (our default default.aspx is to display in English). Next Right-click the Default.aspx.resx file, in the pop-up menu, select Copy, and then in the LocalResources directory, select Paste, so that a new resource file will be copied, renamed to: Default.aspx.zh-cn.resx, This will be used as the Chinese resource file.
Next, we edit the resource file. For example, open the resource file Default.aspx.zh-cn.resx, you will see the following map of the resource file, we can edit the Chinese resource file.
In ASP.net 2.0, simply by adding two new properties "Culture" and "Uicultrue" to the @page page of the page ASPX header, the Web application automatically displays pages of the corresponding language automatically at run time, based on pre-set resource files, As shown in the following program paragraph:
In order to test the program, open IE browser, the language setting selected as Chinese, run the program, you can see that the program automatically calls the Chinese resource file, the displayed page language is Chinese, not the default English (because our default default.aspx is to invoke the English resource file), the following figure:
In the above program, we just take advantage of the nature of the resource file, the following, we will implement let the program "remember" the user each time to choose a language, like Google, remember each user's choice, the next time the user browsing the page, will use the language of the page.
First, to achieve this, we must use the new profile feature provided in ASP.net 2.0. Profile can use the database to store personalized information about the user, a bit like the session object, but the sessions object is a lifetime, after the lifetime, the session object automatically invalidated. Profile is different unless it is explicitly removed. To implement profile functionality, you must first define it in Web.config, as follows:
Profile.language = "en-US"
Profile.Info.LastModified = Now
Profile.Info.DateSelected = Calendar1.selecteddate
In Web.congfig, you will define attributes/values that store the variables and values that will be saved, such as the LastModified property, which defines the value as the Datatime type, and so on. The <group> label, then, puts together variable values that have the same or similar functionality. In Beta 1, Profile uses an Access database to hold these values.
In order for the user to select the language to use each time in the Drop-down box and save it, write the following code in the Submit button:
Sub Btnset_click (ByVal sender as Object, _
ByVal e as System.EventArgs)
Profile.language = DdlLanguage.SelectedItem.Value
End Sub
In order to record the date each time the user chooses the calendar, we use the profile.info.dateselected attribute to record and use the Profile.Info.LastModified to record the time of day when the user selects the date in the calendar, the following code:
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 loads, we can take out the values that were previously saved in the Profile object, and then display the date displayed by the last user in the Calendar control, and the time when the user last selected the date.
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
For each page to load, the following code must be written in the Page_PreInit () event in order to properly display the page based on the language of the previous user-guaranteed profile object:
Dim Lang as System.Globalization.CultureInfo
' Reads the language value selected by the user in the Drop-down box
Dim Selectedlang as String = Request ("Ddllanguage")
' If the user chooses 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, read the previously saved language information
If Selectedlang IsNot Nothing Then
Lang = New System.Globalization.CultureInfo (Selectedlang)
Else
' If this is the first time the page is called
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
Here's how to explain the code. First, declare a variable of type CultureInfo, which saves the user's message each time the language is selected.
Next, first check whether the page is postback, if the postback (occurred in the page refresh or click on the control, fire the page event, then the user may choose a language), then use the Drop-down box in the user's choice of language to initialize the CultureInfo
Lang = New System.Globalization.CultureInfo (Selectedlang)
If the page is the first call, it reads the language information from the original profile object and automatically sets the corresponding display in the Drop-down box (using the case statement) according to what language it is.
Finally, we set the localized language of the system's course to the set Lang, and set the values of each control to the value in the resource file (called resources.resource.xxxx). When the program is running, when the user chooses English, the following illustration shows:
When the user chooses to submit in Chinese, the program runs as shown in the following illustration:
In fact, ASP.net 2.0 is able to achieve the above function, in fact, in the project under the Data directory of a ASPNETDB Access database to record, open the Aspnet_profile table, you will find that it records the user each submitted information.
Summarize:
In this paper, we introduce how to use the profile function of ASP.net 2.0 to implement the local language personalization Web application. The profile function utilizes the database to record the local language information that the user submits each time, which can be easily read to achieve personalized Web pages. We expect the profile feature to be further enhanced in the Visual Studio 2005 Official edition.
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.