Programmatically set Culture of User Controls in ASP.NET
Solution 1
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...
}
Solution 2
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.
Solution 3
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
Dennis Röttger
Former Junior Software Developer, while I switched my career to something more finance-centric I still enjoy developing small apps for friends or as a pastime activity - hope the problems I encountered during my learning process are helpful to you in some way!
Updated on June 04, 2022Comments
-
Dennis Röttger almost 2 years
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
-
Dennis Röttger over 13 yearsThanks for your answer - Just to clarify: I am loading my Culture via LoadControl on an aspx page that has already overridden InitializeCulture, unfortunately my user control still doesn't receive the Culture which is already set on the aspx page it appears in.
-
Frédéric Hamidi over 13 years@Dennis,
InitializeCulture()
is called very early in the page lifecycle, so everything should bet set by the time you load your user control. During which phase do you load it? -
Dennis Röttger over 13 yearsI load it during Page_Load - is there a more appropriate method for it? (I just want to load it once and it doesn't need to be loaded again, that's why I load it in page load and only if the loading occurrence is not a postback)
-
Frédéric Hamidi over 13 years@Dennis, doing it in
Page_Load
is fine, but since loaded user controls are not persisted inViewState
, you probably should load it on every request. -
Dennis Röttger over 13 yearsI've already faced this problem and solved it using an UpdatePanel, the persistence of the Control is of no concern to me at the moment - thanks for your thoughfulness though. However, my Resourceproblem still persists as I can see no way to set the Culture for my Control even though it is set on the aspx page.
-
Frédéric Hamidi over 13 years@Dennis, maybe that's a resource problem. Can you verify that all your
.resx
files have their build action set toEmbedded Resource
in the Visual Studio properties pane, and that your satellite assemblies are properly deployed in subdirectories of yourbin
folder? -
Dennis Röttger over 13 yearsThanks for your persistence - and yes I can, my default .resx is being called, giving my labels/textboxes etc. default names, and my de-DE.resx and en-GB.resx are built in just the same way - if i take the files away, my labels and textboxes are simply empty.
-
Frédéric Hamidi over 13 yearsOh, so your
resx
files are apparently deployed asContent
, notEmbedded Resource
. That's fine, but then they need to reside in anApp_LocalResources
folder at the same level as yourascx
file. Is that the case? -
Dennis Röttger over 13 yearsNo, they weren't - now they are and it works ... I must've checked everything else but forgot the easiest thing - thanks a lot for your persistence!
-
Garr Godfrey over 8 yearsthis may address my problem. I set culture in the InitualizeCulture of the page, but the usercontrols on the page don't respect it.