Template page globalization

Source: Internet
Author: User
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 thatMasterpageClass does not haveInitializecultureMethod for us to override. This is significant becauseInitializecultureMethod is called very early in the page life cycle, and thus is able to affect the initialization of the controls. None of the methods ofMasterpageClass can do this. (The Best We cocould doMasterpageIsOninit, Which is not early enough .)

To get around this problem, we have three choices:

    1. OverrideInitializecultureMethod in the pages that use the master page.
    2. Set the culture in one ofMasterpage'S event handlers and reload the master page to recreate the controls.
    3. 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 havingInitializecultureIn 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 whenBeginrequestExecutes.

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  > 

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.