Introduction
This article explains a page independent way of parameter Ming master page globalization. Implementation Details and complete code snippets are supported ded.
Background
The difficult thing about doing master page globalization is thatMasterpage
Class does not haveInitializeculture
Method for us to override. This is significant becauseInitializeculture
Method is called very early in the page life cycle, and thus is able to affect the initialization of the controls. None of the methods ofMasterpage
Class can do this. (The Best We cocould doMasterpage
IsOninit
, Which is not early enough .)
To get around this problem, we have three choices:
- Override
Initializeculture
Method in the pages that use the master page.
- Set the culture in one of
Masterpage
'S event handlers and reload the master page to recreate the controls.
- Set the culture inGlobal. asax.Global. asaxIs the first thing called upon page request, and thus provides us with a way to affect control creation.
Because the culture of the current thread resets to default on page redirect and other events, the culture setting process has to take place per request. for this reason, the second solution mentioned above is not acceptable for performance and user experience reasons.
Article articles and Forum posts talk about how to implement the first solution. Instead of havingInitializeculture
In each page, a more elegant way is to have a base page that handles the culture setting and is inherited by all the pages in the website/Web application.
In this article, I wocould like to talk about the third solution, which allows developers to simply drop a master page to the website/application and make the culture switch work. implementation details are explained in the following sections.
Setting the culture in global. asax
The first task we have here is setting the culture inGlobal. asax. Cookies are used here because session objects are not accessible in the context whenBeginrequest
Executes.
Global. asax:
Collapse Copy code
Protected Void Application_beginrequest ( Object Sender, eventargs e) {httpcookie cookie = request. Cookies [ " Cultureinfo" ]; If (Cookie! = Null & Cookie. value! = Null ) {Thread. currentthread. currentuiculture = New Cultureinfo (cookie. Value); thread. currentthread. currentculture = New Cultureinfo (cookie. Value );} Else {Thread. currentthread. currentuiculture = New Cultureinfo ( " En-ca" ); Thread. currentthread. currentculture = New Cultureinfo ( " En-ca" );}}
The Code reads the stored culture information from the cookie and uses it, if it's notNull
, To set the current thread culture.
Use a drop down list to change the culture
The next task is to add the drop down list to the master page.
. Master:
Collapse Copy code
< ASP: dropdownlist ID =" Ddllanguage" Runat =" Server" Onselectedindexchanged =" Ddl1_age_selectedindexchanged" Autopostback =" True" > < ASP: listitem Text =" <% $ Resources: resource, users_english %>" Value =" En-ca" / > < ASP: listitem Text =" <% $ Resources: resource, users_french %>" Value =" Fr-ca" / > < / ASP: dropdownlist >
Then, we implement the drop down list event handler that stores the selected culture value in the cookie.
. Master. CS:
Collapse Copy code
Protected Void Ddl1_age_selectedindexchanged ( Object Sender, eventargs e ){ // Sets the cookie that is to be used by global. asax Httpcookie cookie = New Httpcookie ( " Cultureinfo" ); Cookie. value = ddllanguage. selectedvalue; response. Cookies. Add (cookie ); // Set the culture and reload the page for immediate effect. // Future effects are handled by global. asax Thread. currentthread. currentculture = New Cultureinfo (ddllanguage. selectedvalue); thread. currentthread. currentuiculture =New Cultureinfo (ddllanguage. selectedvalue); server. Transfer (request. Path );}
As stated in the comments above, setting the culture and specify Ming a reload forces the thread culture to change immediately.
Display the current culture in the drop down list
Finally, we wowould like the drop down list to have the current culture as the selected value.
. Master. CS:
Collapse Copy code
Protected VoidPage_load (ObjectSender, eventargs e ){//Only does it on non-PostBack because otherwise the selected//Value will not reach event handler correctlyIf(! Page. ispostback) {ddllanguage. selectedvalue = thread. currentthread. currentculture. Name ;}}
Note that it only happens for non-PostBack events. if we do it for PostBack events as well, the user selection will be overridden before it reaches the event handler. another way of getting around this problem is by polling the selected value of the drop down list inGlobal. asax.
Complete code snippets
Global. asax:
Collapse Copy code
Protected Void Application_beginrequest ( Object Sender, eventargs e) {httpcookie cookie = request. Cookies [ " Cultureinfo" ]; If (Cookie! = Null & Cookie. value! = Null ) {Thread. currentthread. currentuiculture = New Cultureinfo (cookie. Value); thread. currentthread. currentculture = New Cultureinfo (cookie. Value );} Else {Thread. currentthread. currentuiculture = New Cultureinfo ( " En-ca" ); Thread. currentthread. currentculture = New Cultureinfo ( " En-ca" );}}
. Master. CS:
Collapse Copy code
Protected Void Page_load ( Object Sender, eventargs e ){ // Only does it on non-PostBack because otherwise // The selected value will not reach event handler correctly If (! Page. ispostback) {ddllanguage. selectedvalue = thread. currentthread. currentculture. Name ;}} Protected Void Ddl1_age_selectedindexchanged ( Object Sender, eventargs e ){ // Sets the cookie that is to be used by global. asax Httpcookie cookie = New Httpcookie ( " Cultureinfo" ); Cookie. value = ddllanguage. selectedvalue; response. Cookies. Add (cookie ); // Set the culture and reload for immediate effect. // Future effects are handled by global. asax Thread. currentthread. currentculture = New Cultureinfo (ddllanguage. selectedvalue); thread. currentthread. currentuiculture = New Cultureinfo (ddllanguage. selectedvalue); server. Transfer (request. Path );}
. Master:
Collapse Copy code
< ASP: dropdownlist ID =" Ddllanguage" Runat =" Server" Onselectedindexchanged =" Ddl1_age_selectedindexchanged" Autopostback =" True" > < ASP: listitem Text =" <% $ Resources: resource, users_english %>" Value =" En-ca" / > < ASP: listitem Text =" <% $ Resources: resource, users_french %>" Value =" Fr-ca" / > < / ASP: dropdownlist >