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.
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:
- 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
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..
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.
Marquis_DeBl...
Member
67 Points
51 Posts
Can I call Initialize Culture of every page from the Global.asax?
Jan 24, 2011 10:28 PM|LINK
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?
InitializeCulture Global.asax
hassanmehmoo...
Star
8970 Points
1590 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Jan 25, 2011 04:15 AM|LINK
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..
Mark as Answer, if it answers you..
--
Marquis_DeBl...
Member
67 Points
51 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Jan 25, 2011 02:05 PM|LINK
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.
Inheritance global functions. hassanmehmood
Marquis_DeBl...
Member
67 Points
51 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Jan 25, 2011 02:14 PM|LINK
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.
hassanmehmoo...
Star
8970 Points
1590 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Jan 25, 2011 02:52 PM|LINK
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..
Mark as Answer, if it answers you..
--
Marquis_DeBl...
Member
67 Points
51 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Jan 26, 2011 05:55 PM|LINK
Great.
akhillal
Member
5 Points
13 Posts
Re: Can I call Initialize Culture of every page from the Global.asax?
Feb 21, 2013 09:33 AM|LINK
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