masterpage initializeculture no suitable method found to override error?

后端 未结 2 1792
离开以前
离开以前 2020-12-30 08:06

I\'m trying to develop a MultiLanguage web site using ASP.NET with C# My problem is: I want to make my MasterPage support switching among languages, but when i put the \"Ini

2条回答
  •  醉梦人生
    2020-12-30 08:20

    There is an alternate solution that DOES NOT require you to create a BasePage.

    The problem with 'Culture' is that it gets set very very early on the page life cycle, so the Page.InitializeCulture event is one of the early events on the page (if not the only one) where we can hook up to change the Thread.CurrentThread.CurrentUICulture. But what if we do that even earlier, as soon as the Request begins on the server.

    I do that on the Application_BeginRequest event on the Global.asax file which is called on every request.

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
      HttpCookie cookie = Request.Cookies["langCookie"];
      if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
      {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
      }
    }
    

    There I check for the existence of a cookie that holds the culture I want to use. If there's no cookie, then the default culture will be used.

    To change the language on my application I just need a control that changes the cookie value client-side and then do a simple postback to the server. It doesn't matter if such control is on the Content Page or in the Master Page, it doesn't even need any code on the server-side, because all the processing is done on the method above, and the cookie gets set on the client-side even before the page is posted.

    I've used a simple LinkButton (which is styled as a Mexican Flag), but you can use any other control that do a postback when clicked/changed.

    
    

    Right before this button posts back to the server, it runs the client-side click event which updates the cookie value I want to set, and voila!

    I have the javascript code that sets the cookie at the Master Page head section:

    function SetLanguageCookie(selectedLanguage) {
      var expDate = new Date();
      expDate.setDate(expDate.getDate() + 20); // Expiration 20 days from today
      document.cookie = "langCookie=" + selectedLanguage + "; expires=" + expDate.toUTCString() + "; path=/";
    };
    

    That's it!! The Thread.CurrentThread.CurrentUICulture gets changed and no BasePage class is needed nor overriding the Page.InitializeCulture method. There's even the side effect that the language selected is remembered on following visits since it's stored on a cookie.

    If you want to use a DropDownList instead of a LinkButton, just make sure to set the AutoPostBack="true" and, since there is no OnClientChanged property for the DropDownList, you must hardcode the onchange attribute on the DropDownList and pass the selected value to the same javascript function.

    
      
      
      
    
    

    The onchange attribute is not part of the DropDownList properties, however, since the DropDownList is an analog control of the

    Hope someone finds this approach as useful as I do. :)

提交回复
热议问题