在 ASP.NET 中以编程方式设置用户控件的区域性

发布于 2024-10-15 08:44:46 字数 316 浏览 0 评论 0原文

我想以编程方式设置用户控件的文化,它定义了几个标签、按钮和文本框...

通常,对于 aspx 页面,您覆盖 InitializeCulture-Method 并设置 Culture 和 UICulture 来实现此目的,唉 ASCX-Controls 这样做没有这个方法,那么我到底该怎么做呢?

我已经设置了本地资源 mycontrol.ascx.de-DE.resx、mycontrol.ascx.resx 和 mycontrol.ascx.en-GB.resx,但仅使用默认文件 (mycontrol.ascx.resx) 的值。

提前致谢。

丹尼斯

I'd like to programmatically set the culture of my User Control which defines several Labels, Buttons and Textboxes ...

Usually, for aspx-pages you override the InitializeCulture-Method and set Culture and UICulture to achieve this, alas ASCX-Controls do not have this Method, so how exactly would I do this?

I've set up the local resources mycontrol.ascx.de-DE.resx, mycontrol.ascx.resx and mycontrol.ascx.en-GB.resx but only the values of the default file (mycontrol.ascx.resx) are used.

Thanks in advance.

Dennis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又爬满兰若 2024-10-22 08:44:46

当前区域性是线程范围的: Page.CulturePage.UICulture 实际上设置了 Thread.CurrentThread.CurrentCultureThread.CurrentThread .CurrentUICulture 底层。

如果用户控件是在页面标记中定义的,我认为您无能为力。如果使用 LoadControl() 加载它,您可以在调用之前暂时覆盖当前线程的区域性,并在调用之后恢复它,但这会很尴尬:

protected void Page_Load(object sender, EventArgs e)
{
    // Some code...

    CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture;
    CultureInfo oldUICulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentCulture = yourNewCulture;
    Thread.CurrentThread.CurrentUICulture = yourNewUICulture;

    try {
        Controls.Add(LoadControl("yourUserControl.ascx"));
    } finally {
        Thread.CurrentThread.CurrentCulture = oldCulture;
        Thread.CurrentThread.CurrentUICulture = oldUICulture;
    }

    // Some other code...
}

The current culture is thread-wide: Page.Culture and Page.UICulture actually set Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture under the hood.

If the user control is defined in your page markup, I don't think there's anything you can do. If you load it with LoadControl(), you can temporarily override the current thread's culture before the call and restore it afterward, but it would be quite awkward:

protected void Page_Load(object sender, EventArgs e)
{
    // Some code...

    CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture;
    CultureInfo oldUICulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentCulture = yourNewCulture;
    Thread.CurrentThread.CurrentUICulture = yourNewUICulture;

    try {
        Controls.Add(LoadControl("yourUserControl.ascx"));
    } finally {
        Thread.CurrentThread.CurrentCulture = oldCulture;
        Thread.CurrentThread.CurrentUICulture = oldUICulture;
    }

    // Some other code...
}
梦归所梦 2024-10-22 08:44:46

我也花了几个小时来解决这个问题,终于得到了这个解决方案。
您只需重写 FrameworkInitialize() 而不是 initilizeculture()
例如:

protected override void FrameworkInitialize()
{
    String selectedLanguage = LanguageID;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

    base.FrameworkInitialize();
}

只要将此代码放入您的ascx.cs文件中,则无需调用此覆盖函数。

I also spent hours on this problem and finally got This solution.
You only have to override the FrameworkInitialize() instead of initilizeculture(),
eg:

protected override void FrameworkInitialize()
{
    String selectedLanguage = LanguageID;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

    base.FrameworkInitialize();
}

Jus Put this code inside your ascx.cs file, This override function does not need to be called.

站稳脚跟 2024-10-22 08:44:46

我使用以下代码在项目页面中更改 asp.net mvc 的语言

public ActionResult ChangeCulture (Culture lang, string returnUrl)
     {
         if (returnUrl.Length> = 3)
         {
             returnUrl = returnUrl.Substring (3);
         }
         return redirect ("/" + lang.ToString () + returnUrl);
     }

,还在同一页面上使用了用户控件 (ascx)。当我使用操作链接更改语言时,视图页面语言已更改,但用户控件页面加载事件未捕获更改因此视图页面的语言发生了变化,但用户控件的语言没有改变

I used the following code to change the language for asp.net mvc in my project page

public ActionResult ChangeCulture (Culture lang, string returnUrl)
     {
         if (returnUrl.Length> = 3)
         {
             returnUrl = returnUrl.Substring (3);
         }
         return redirect ("/" + lang.ToString () + returnUrl);
     }

also I used usercontrol (ascx) on the same page.when I changed language with actionlink is changed viewpage language but user control pageload event isn't capture changing so viewpage's language changes but usercontrol's language doesn't change

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文