locked
Can I call Initialize Culture of every page from the Global.asax? RRS feed

  • Question

  • User1456393972 posted

    Can I call Initialize Culture of every page from the Global.asax?

    Every page looks at the query string of "?lang=##" and sets the language accordingly.  Since all pages do this, is there a place where I can implement it once and all pages will load it?

    My site has around 200 pages and I dont want to implement the code in everypage.  Would take too long for maintenance and to implement.  It would be more convinient if I could simply put it in a function that all pages call when loading.

    Is this possible?

    Monday, January 24, 2011 6:28 PM

Answers

  • User-1364446067 posted

    Hi.

    I think it will not be possible to call Initialize Culture of every Page from Global.asax.

    But you can achieve this thing by the following:

    - Make a Page (say Main.aspx) and override InitializeCulture method of page in it. Initialize the culture using your Query String parameter.. like this:

    protected override void InitializeCulture()
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture =
                    System.Globalization.CultureInfo.CreateSpecificCulture(Request.QueryString["lang"]);
    
                base.InitializeCulture();
            }


    - Inherit all of your other Pages from this page i.e. Main.aspx.. Like this..

    public partial class Page1 : Main
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    
    public partial class Page2 : Main
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }


    - Now whenever every page will be requested the InitializeCulture will be invoked automatically and will set the culture for the requested Page using the Query String value..


    - This method won't require much in code maintenance and updations too



    --

    Hope this helps..


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 12:15 AM
  • User-1364446067 posted

    Hi Marquis, Glad to know that my solution worked.

    Yes you can leave this default markup code as it is. It won't have any effect on the children pages.

    the title tag had no concern as well, as this page is never requested directly. Its always one of its children pages which is requested and which will have its own title tag..


    --

    Hope this helps..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 10:52 AM

All replies

  • User-1364446067 posted

    Hi.

    I think it will not be possible to call Initialize Culture of every Page from Global.asax.

    But you can achieve this thing by the following:

    - Make a Page (say Main.aspx) and override InitializeCulture method of page in it. Initialize the culture using your Query String parameter.. like this:

    protected override void InitializeCulture()
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture =
                    System.Globalization.CultureInfo.CreateSpecificCulture(Request.QueryString["lang"]);
    
                base.InitializeCulture();
            }


    - Inherit all of your other Pages from this page i.e. Main.aspx.. Like this..

    public partial class Page1 : Main
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    
    public partial class Page2 : Main
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }


    - Now whenever every page will be requested the InitializeCulture will be invoked automatically and will set the culture for the requested Page using the Query String value..


    - This method won't require much in code maintenance and updations too



    --

    Hope this helps..


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 12:15 AM
  • User1456393972 posted

    Thanks hassanmehmood!


    That is exactly what I was tryting to do!  This will be useful for so many other things too and save me alot of work.  Thanks alot.  That worked great!!


    I didnt know I could inherit webpages like that from one another.

    cool.

    Tuesday, January 25, 2011 10:05 AM
  • User1456393972 posted

    hasseanmehmood,


    Do I just leave the html code in the parent .aspx file.  Will this have an effect on the content of the child page that inherets from the parent?

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Parent.aspx.cs" Inherits="Test_UICultureHandler.Parent" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            
        </div>
        </form>
    </body>
    </html>

    I just deleted the <title></title> tag.

    Tuesday, January 25, 2011 10:14 AM
  • User-1364446067 posted

    Hi Marquis, Glad to know that my solution worked.

    Yes you can leave this default markup code as it is. It won't have any effect on the children pages.

    the title tag had no concern as well, as this page is never requested directly. Its always one of its children pages which is requested and which will have its own title tag..


    --

    Hope this helps..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 10:52 AM
  • User1456393972 posted

    Great. 

    Wednesday, January 26, 2011 1:55 PM
  • User-1433024157 posted

    Hi

    I am working on a web site which support english and arabic languages. I have an ajax accordion control inside a content page, and it works fine by default . I  just place the language change link in master page. when i switch to another language , accordian control stops working.

    but when i click on any other button ( other than language change link ) in master page / content page , this magical accordion will statrs working again..  i don't  know what is happening. why it is not working once i switch to other language.

    Thanks

    Thursday, February 21, 2013 5:33 AM