Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 母版页初始化文化找不到覆盖错误的合适方法?_C#_Calendar - Fatal编程技术网

C# 母版页初始化文化找不到覆盖错误的合适方法?

C# 母版页初始化文化找不到覆盖错误的合适方法?,c#,calendar,C#,Calendar,我正在尝试使用ASP.NET和C开发一个多语言网站# 我的问题是:我想让我的母版页支持在不同语言之间切换,但是当我将“InitializeCulture()”放在MasterPage.cs中时,我遇到了这个错误 这是我的代码: public partial class BasicMasterPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { } protected vo

我正在尝试使用ASP.NET和C开发一个多语言网站# 我的问题是:我想让我的母版页支持在不同语言之间切换,但是当我将“InitializeCulture()”放在MasterPage.cs中时,我遇到了这个错误

这是我的代码:

public partial class BasicMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsToday)
    {
        e.Cell.Style.Add("background-color", "#3556bf");
        e.Cell.Style.Add("font-weight", "bold");
    }
}
Dictionary<string, System.Globalization.Calendar> Calendars =
    new Dictionary<string, System.Globalization.Calendar>()
    {
        {"GregorianCalendar", new GregorianCalendar()},
        {"HebrewCalendar", new HebrewCalendar()},
        {"HijriCalendar", new HijriCalendar()},
        {"JapaneseCalendar", new JapaneseCalendar()},
        {"JulianCalendar", new JulianCalendar()},
        {"KoreanCalendar", new KoreanCalendar()},
        {"TaiwanCalendar", new TaiwanCalendar()},
        {"ThaiBuddhistCalendar", new ThaiBuddhistCalendar ()}
    };

protected override void InitializeCulture()
{
    if (Request.Form["LocaleChoice"] != null)
    {
        string selected = Request.Form["LocaleChoice"];
        string[] calendarSetting = selected.Split('|');
        string selectedLanguage = calendarSetting[0];

        CultureInfo culture = CultureInfo.CreateSpecificCulture(selectedLanguage);

        if (calendarSetting.Length > 1)
        {
            string selectedCalendar = calendarSetting[1];
            var cal = culture.Calendar;
            if (Calendars.TryGetValue(selectedCalendar, out cal))
                culture.DateTimeFormat.Calendar = cal;
        }

        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
    }
    base.InitializeCulture();
}
}
公共部分类BasicMasterPage:System.Web.UI.MasterPage
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
如果(例如,每天)
{
e、 Cell.Style.Add(“背景色”,“3556bf”);
e、 单元格。样式。添加(“字体大小”、“粗体”);
}
}
字典日历=
新字典()
{
{“GregorianCalendar”,新GregorianCalendar()},
{“HebrewCalendar”,新的HebrewCalendar()},
{“HijriCalendar”,新的HijriCalendar()},
{“JapaneseCalendar”,新的JapaneseCalendar()},
{“JulianCalendar”,新的JulianCalendar()},
{“朝鲜历法”,新朝鲜历法(),
{“台湾历”,新台湾历(),
{“ThaiBuddhistCalendar”,新ThaiBuddhistCalendar()}
};
受保护的覆盖无效初始化文化()
{
if(Request.Form[“LocaleChoice”]!=null)
{
所选字符串=Request.Form[“LocaleChoice”];
string[]calendarSetting=选中。拆分(“|”);
字符串selectedLanguage=calendarSetting[0];
CultureInfo culture=CultureInfo.CreateSpecificCulture(selectedLanguage);
如果(calendarSetting.Length>1)
{
字符串selectedCalendar=calendarSetting[1];
var-cal=culture.Calendar;
if(Calendars.TryGetValue(selectedCalendar,out cal))
culture.DateTimeFormat.Calendar=cal;
}
Thread.CurrentThread.CurrentCulture=区域性;
Thread.CurrentThread.CurrentUICulture=区域性;
}
base.InitializeCulture();
}
}

如何创建基类?

方法
InitializeCulture()
只存在于类上,而不存在于类上,这就是为什么会出现该错误

要解决此问题,您可以创建一个所有特定页面都继承的
BasePage

  • 创建一个新类(不是Webform),称它为
    BasePage
    ,或者任何你想要的
  • 让它继承
  • 使所有其他页面继承
    BasePage
  • 下面是一个例子:

    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            //Do the logic you want for all pages that inherit the BasePage.
        }
    }
    
    具体页面应如下所示:

    public partial class _Default : BasePage //Instead of it System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Your logic.
        }
    
        //Your logic.
    }
    

    有一种替代解决方案不需要您创建基本页。

    “Culture”的问题是,它在页面生命周期的很早就被设置,因此
    page.InitializeCulture
    事件是页面上的早期事件之一(如果不是唯一事件的话),我们可以在其中连接以更改
    Thread.CurrentThread.CurrentUICulture
    。但是,如果我们更早地这样做,一旦服务器上的请求开始,该怎么办呢

    我在
    Global.asax
    文件上的
    Application\u BeginRequest
    事件中这样做,该文件在每次请求时都会被调用

    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);
      }
    }
    
    在那里,我检查是否存在保存我想要使用的文化的cookie。如果没有cookie,则将使用默认区域性

    要更改应用程序上的语言,我只需要一个控件来更改客户端的cookie值,然后向服务器进行简单的回发。不管这种控制是在内容页还是母版页上,它甚至不需要服务器端的任何代码,因为所有的处理都是在上面的方法上完成的,而且cookie甚至在页面发布之前就在客户端设置好了

    我使用了一个简单的LinkButton(样式为墨西哥国旗),但您可以使用任何其他控件在单击/更改时进行回发

    <asp:LinkButton ID="btnSpanish" runat="server" OnClientClick="SetLanguageCookie('es')" CausesValidation="false" CssClass="mxFlag" />
    
    就这样!!
    Thread.CurrentThread.CurrentUICulture
    已更改,不需要
    BasePage
    类,也不重写
    Page.InitializeCulture
    方法。甚至还有一个副作用,那就是所选语言在后续访问中会被记住,因为它存储在cookie中

    如果要使用
    DropDownList
    而不是
    LinkButton
    ,请确保设置
    AutoPostBack=“true”
    ,并且由于
    DropDownList
    没有
    OnClientChanged
    属性,必须对
    DropDownList
    上的
    onchange
    属性进行硬编码,并将所选值传递给相同的javascript函数

    <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>
    

    希望有人发现这种方法和我一样有用。:)

    我也在寻找一种方法来实现这一点,所以我不能给你一个解决方案,但我可以告诉你方法
    InitializeCulture()
    只存在于类上,而不存在于类上,这就是为什么你会出现这个错误。这个方法工作得很好,没有引起任何错误。但日历的工作已经停止;每当我从组合框中选择一种日历类型时,它永远不会出现。为什么?@Nuha_IT我建议问一个新的问题,用新的具体词汇problem@AskeB. 那么您的意思是让母版页也继承
    BasePage
    ?因为默认情况下它继承了
    System.Web.UI.MasterPage
    ?所有其他页面都能完美地本地化它唯一的
    母版页
    ,这是
    OP
    在第一个页面中的问题place@jaminator不,母版页不应继承
    基本页
    ,因为
    基本页
    System.Web.UI.page
    而不是
    System.Web.UI.母版页
    。此解决方案是为多个
    页面
    创建功能的一种方法。OP希望通过调用
    initializeCulture()
    来支持所有站点上的语言更改。由于该方法是在
    页面
    类上定义的(据我所知,
    母版页
    没有等价物),因此我定义了一个
    页面
    -包装类,可以对多个页面执行相同的功能。母版页本地化的基本OOD
    <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>
    
    <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>