It's asp.net2.0. A new declarative expression syntax that replaces a value to a page before parsing the page. ASP. NET expressions are a declarative way to set control properties based on information that is calculated at run time. Asp. NET expressions are mainly used in: connection strings, application settings, resource files, and so on.
1. First of all, the following knowledge should be understood:
Understand a noun: expression syntax
Expression syntax format: <%$ ...%>
It's asp.net2.0. A new declarative expression syntax that replaces a value to a page before parsing the page.
Asp. NET expressions are a declarative way to set control properties based on information that is calculated at run time.
Asp. NET expressions are mainly used in: connection strings, application settings, resource files, and so on.
The basic syntax for an asp.net expression is as follows:
<%$ Expressionprefix:expressionvalue%>
The following shows the application of expression syntax in connection strings and application settings:
A. Application of expression syntax in connection strings
Sets the value of the connection string stored in the Web.config file to the connection string property of the control
<asp:sqldatasource id= "SqlDataSource1" runat= "Server"
Selectcommand= "SELECT * from Employees"
connectionstring= "<%$ Connectionstrings:northwindcon%>" >
</asp:SqlDataSource>
<configuration>
<connectionStrings>
<add name= "Northwindcon"
connectionstring= "Data source=yanfa0;integrated security=sspi;initial ctalog=northwind;"
Providername= "System.Data.SqlClient"/>
</connectionStrings>
</configuration>
B. Expression syntax applied to application settings
Use an expression to reference the application settings defined in the Web.config configuration file
<asp:label id= "Label1" runat= "Server" text= "<%$ appsettings:txt%>" ></asp:Label>
<appSettings>
<add key= "Txt" value= "ABC"/>
</appSettings>
In fact, there is an important application of expression syntax in resource files, and resource files are the basis of my localization.
The format settings for the culture
The culture name and identifier name follow the RFC 1766 standard, please see MSDN
Take a look at the image below to capture some of the regional pictures:
Cultures are typically grouped into three types: the invariant culture, the neutral culture, and the specific culture.
A. The invariant culture is not culture-sensitive. You can specify the invariant culture by using an empty string ("") or by a culture identifier 0x007f, which is associated with the English language but not with any country/region.
B. A neutral culture is a culture that is associated with a language but is not associated with a country/region.
C. A specific culture is the culture associated with a language and a country/region.
For example, "fr" (French) is a neutral culture, and "Fr-fr" (French (France)) is a specific culture
Special note: "Zh-chs" (Simplified Chinese) and "Zh-cht" (traditional Chinese) are neutral cultures.
Cultures are hierarchical, that is, the parent of a specific culture is a neutral culture, and the parent of a neutral culture is InvariantCulture
With asp.net2.0, you can easily change the culture settings on a per-page basis
Simply add the UICulture and culture (culture) attributes to. aspx and other similar pages within the page command can be
<%@ Page culture= "Auto" uiculture= "Auto"%>
To set the same culture setting for all pages in the site, simply add the following elements to the Web.config file at the root of the site so that you do not have to assign each page separately
<globalization uiculture= "Auto" culture= "Auto"/>
You can also specify a default culture for ASP.net
The user interface culture for the specified page below is automatic, the default culture is English, the page's culture is automatic, and the default culture is English (United States)
<globalization uiculture= "Auto:en" culture= "auto:en-us"/>
Culture: Indicates the culture setting for the page
UICulture: Specifies the user interface (UI) culture settings for the page
B. Setting language preferences
The default culture and UICulture selected by the automatic detection feature may sometimes not be required by the user
For example: An American salesman came to China on business and went online with a business bill at China's headquarters. In this case, the Web application should provide the user with the ability to explicitly change the language, providing him with an English version of the business under one page
Using System.Threading;
Using System.Globalization;
protected override void InitializeCulture ()
{
The explicit designated area is EN, which shows the English page for the American salesman.
In practical application, the language can be set dynamically according to user's choice.
You can also use profile to remember the language the user chooses, so that users automatically enter the appropriate language page when they visit the site later.
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("en-us");
}
Note: Changes to Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture need to be made in the InitializeCulture () method. Because automatic detection of the preferred browser language occurs in the early part of the page lifetime
Special tips:
The instantiation of the CultureInfo class typically has two ways, as follows:
CultureInfo Culture = CultureInfo. CreateSpecificCulture (name);
CultureInfo Culture = new CultureInfo (name);
The distinction between the two:
Using the first method, you can only create a CultureInfo instance of a fixed culture or a specific culture.
Using the second method, you create a CultureInfo instance of the culture specified by name, which can be fixed, neutral, or specific to a culture.
The CurrentCulture property of the thread class is used to get or configure the culture of the current thread, which must be configured as a specific culture.
If Thread.CurrentThread.CurrentCulture = new CultureInfo ("en"), it will be an error!
2. Multi-language implementation
In fact, the implementation is relatively simple, only need the following steps
1. Create a global Resource table
2. Store variables that represent the current culture in the session, such as "en-us", "ZH-CN"
3. Create base class Pagebase for all pages, overwriting initializeculture functions
4. Text values on the page, assignment of expression syntax
Add two global resource tables
Strings.resx and Strings.en-us.resx (note-The following characters should conform to RFC 1766 standard, the default value is not with suffix)
The following figure:
Pagebase
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using system.web;
5using System.Globalization;
6///<summary>
7///Summary description for Pagebase
8///</summary>
9public class PageBase:System.Web.UI.Page
10{
One public pagebase ()
12 {
13//
//Todo:add constructor logic here
15//
16}
protected override void InitializeCulture ()
18 {
String currentCulture = (string) session["Culture"];
The IF (string. IsNullOrEmpty (currentCulture))
21 {
CurrentCulture = "ZH-CN";
session["Culture"] = "ZH-CN";
24}
25
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo (currentCulture);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture ( CurrentCulture);
28
29}
30}
31
Basic page:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using system.web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7using System.Globalization;
8public partial class _default:P agebase
9{
protected void Page_Load (object sender, EventArgs e)
11 {
12
13}
protected void Btnchinese_click (object sender, EventArgs e)
15 {
session["Culture"] = "ZH-CN";
17/////can try to comment on the effect of the following sentence
this. RegisterClientScriptBlock ("Reload", "<script>window.location=window.self.location;</script>");
19}
protected void Btnenglish_click (object sender, EventArgs e)
21 {
session["Culture"] = "en-US";
23/////can try to comment on the effect of the following sentence
this. RegisterClientScriptBlock ("Reload", "<script>window.location=window.self.location;</script>");
25}
26}
27
Simple example of a multilingual
Disadvantage: Need to load two times when switching languages
Recommendation: You can use AJAX request to modify the session value after the implementation of Window.location=window.self.location;
Such as:
1 function Pagerefresh (language)
2 {
3//using jquery to make AJAX requests
4 Try
5 {
6
7 var virthpath= "Http://localhost/MulitLanguage";
8
9
Ten $.ajax ({
One type: "POST",
url:virthpath+ "/ajaxhandler.ashx", change the session value in///AJAXHANDLER.ASHX
Success:function (msg) {
CHANGELANGUAGE_RSLT (msg);
15}
16});
17
18
19}
catch (E)
21 {
Alert (' Error ');
23}
24
25
26}
27
function Changelanguage_rslt (data)
29 {
if (data== "OK")
Window.location=window.self.location;
32
33}
Or else
35 {
+ alert (data);
37}
38}