locked
Web page acting strange RRS feed

  • Question

  • User172054312 posted

    Hello everybody,

    I have a web page named Default.aspx for example. In the code behind in Page_PreInit method I have this code.

    protected void Page_PreInit(object sender, EventArgs e)
            {
                Session["PreferredCulture"] = "fa-IR";
                if (Session["PreferredCulture"] == null)
                    Session["PreferredCulture"] = Request.UserLanguages[0];
                string UserCulture = Session["PreferredCulture"].ToString();
                if (UserCulture != "")
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
                }
            }

    In other page I have named Home.aspx, this code works fine and all controls properties will be read from the resource file Home.aspx.fa.resx. But here in Default.aspx this code will not cause control properties to be read from Default.aspx.fa.resx resource file in App_LocalResources folder.

    I have written this code in Default.aspx page's Page_Load method to see whether the culture is being set correctly and it showed me that the culture is "fa-IR".

    protected void Page_Load(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Write(Resources.StyleSheets.Default);
                Response.End();
            }

    And this code showed me '/Styles/Default.fa.css' which is the value set for the Default key in StyleSheets.fa.resx resource file in App_GlobalResources folder. This shows that the culture has been set correctly.

    When I set the Culture and UICulture in <%@ Page ... section in the page, it works fine and every property will be read from Default.aspx.fa.resx! There should be a problem in the code behind so that when controls are being rendered, the culture is 'en-US' yet!

    There's another strange thing. Every single control's ClientID is exactly the same as its server-side ID. I don't know whether this is a problem or not.

    Any idea would be appreciated.

    Regards,
    Shakib

    Monday, January 17, 2011 3:07 AM

Answers

  • User172054312 posted

    Hi,

    I have found my problem. I used to set the culture in Page_PreInit method. But I have to override InitializeCulture method and write the code there. The Page_PreInit method is suitable for setting the Theme not setting the culture!

    protected override void InitializeCulture()
            {
                Session["PreferredCulture"] = "fa-IR";
                if (Session["PreferredCulture"] == null)
                    Session["PreferredCulture"] = Request.UserLanguages[0];
                string UserCulture = Session["PreferredCulture"].ToString();
                if (UserCulture != "")
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
                }
            }

    This code will work correctly.

    Thanks,
    Shakib

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 17, 2011 5:42 AM