Search code examples
c#localizationuser-controlsasmx

Localization of dynamic rendered user control


Im using the following code to load a page dynamically from JavaScript

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetSimpleControl(string usercontrolName)
{
    Page page = new Page();
    UserControl ctl = (UserControl)page.LoadControl(usercontrolName);

    page.Controls.Add(ctl);
    StringWriter writer = new StringWriter();
    HttpContext.Current.Server.Execute(page, writer, false);
    return writer.ToString();
}

My problem is, that it's not localized, but always uses the default language. In the user control I'm loading, I try to call InitializeCulture and set the culture:

System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo

How do I accomplish this?


Solution

  • Got it to work by setting the UICulture and Culture of the page. The GetSimpleControl now looks like this

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetSimpleControl(string usercontrolName)
    {
        Page page = new Page();
        page.Culture = "en";
        page.UICulture = "en";
        UserControl ctl = (UserControl)page.LoadControl(usercontrolName);
    
        page.Controls.Add(ctl);
        StringWriter writer = new StringWriter();
        HttpContext.Current.Server.Execute(page, writer, false);
        return writer.ToString();
    

    }