Programmatically set Culture of User Controls in ASP.NET

Dennis Röttger picture Dennis Röttger · Feb 2, 2011 · Viewed 11.3k times · Source

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

Answer

SIbghat picture SIbghat · Nov 20, 2012

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.