Tuesday, March 13, 2012

Setting Thread.CurrentThread.CurrentCulture problem

Hi all!
Here is a simple page to change the current CultureInfo of the thread at runtime.
When I click a button, the change only takes effect in the PerRender event.
When the page is loaded again, the current CultureInfo of the thread is again set to the default language of windows.
I do not have any globalization settings in the web.config. I did not set the Culture attribute in the page directive.
The Thread.CurrentThread.CurrentCulture seems to be reset in each request.

Where is the right place to set the current CultureInfo in ASP.NET 2.0 at runtime?
Do I need a special setting of the Culture attribute in the page directive?
Thanks in advance
- Ulrich Schumacher

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="SetLanguage.aspx.cs" Inherits="SetLanguage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnEnglish" Text="English" runat="server" OnClick="btnEnglish_Click" />
<asp:Button ID="btnDeutsch" Text="Deutsch" runat="server" OnClick="btnDeutsch_Click" />
<asp:Label ID="lblTest" runat="server" Text="<% $Resources: Strings, lblTest %>"></asp:Label>
</div>
</form>
</body>
</html>
public partial class SetLanguage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Response.Write("Page_Load: " + Thread.CurrentThread.CurrentCulture.ToString() + "<br>");
}

protected void Page_PreRender(object sender, EventArgs e)
{
this.Response.Write("Page_PreRender: " + Thread.CurrentThread.CurrentCulture.ToString() + "<br>");
}

protected void btnEnglish_Click(object sender, EventArgs e)
{
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
protected void btnDeutsch_Click(object sender, EventArgs e)
{
CultureInfo ci = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
}

See post inhttp://forums.asp.net/1001532/ShowPost.aspx

0 comments:

Post a Comment