masterpage initializeculture no suitable method found to override error?

后端 未结 2 1793
离开以前
离开以前 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:13

    The method InitializeCulture() exists only on the Page class, not the MasterPage class, and that's why you get that error.

    To fix this, you could create a BasePage that all your specific pages inherit:

    1. Create a new Class (not Webform), call it BasePage, or whatever you want.
    2. Make it inherit System.Web.UI.Page.
    3. Make all your other pages inherit the BasePage.

    Here's an example:

    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            //Do the logic you want for all pages that inherit the BasePage.
        }
    }
    

    And the specific pages should look something like this:

    public partial class _Default : BasePage //Instead of it System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Your logic.
        }
    
        //Your logic.
    }
    
    0 讨论(0)
  • 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.

    <asp:LinkButton ID="btnSpanish" runat="server" OnClientClick="SetLanguageCookie('es')" CausesValidation="false" CssClass="mxFlag" />
    

    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.

    <asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="true" onchange="SetLanguageCookie(this.options[this.selectedIndex].value)">
      <asp:ListItem Text="English" Value="en" />
      <asp:ListItem Text="Español" Value="es" />
      <asp:ListItem Text="Français" Value="fr" />
    </asp:DropDownList>
    

    The onchange attribute is not part of the DropDownList properties, however, since the DropDownList is an analog control of the <select> control, the attribute is just placed 'as is' when the rendering happens, and it's rendered before the postback mechanism code. Here's the HTML rendered by the DropDownList above:

    <select name="ctl00$cph1$ddlLanguage" onchange="SetLanguageCookie(this.options[this.selectedIndex].value);setTimeout(&#39;__doPostBack(\&#39;ctl00$cph1$ddlLanguage\&#39;,\&#39;\&#39;)&#39;, 0)" id="cph1_ddlLanguage">
      <option value="en">English</option>
      <option value="es">Español</option>
      <option value="fr">Français</option>
    </select>
    

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

    0 讨论(0)
提交回复
热议问题