Click here to Skip to main content
12,699,214 members (23,751 online)
Click here to Skip to main content
Add your own
alternative version


100 bookmarked

Developing an ASP.NET page with MasterPage and Localization

, 15 Nov 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
The MasterPage is derived from UserControl and thus, does not support the method 'InitializeCulture()'; a bit more coding is required in order to make the ASP.NET MasterPage localizable.


While seeking on the internet for a solution to implement localization within an ASP.NET application using a MasterPage, I realized that a lot of people have got the same problem to solve. Unfortunately, I could not find a suitable solution thus, I intended to do my own implementation.


The solution presented within this article uses the standard localization mechanism of the .NET framework.

Using the code

The published solution uses the Session object as storage for the currently selected culture. This will be initialized during the Session_Start method that is part of the global.asax file.

If a culture change is requested by the user, the MasterPage changes the stored culture in the Session object.

In a BasePage that inherits from Page, the method InitializeCulture is overridden and sets the appropriate culture information stored in the Session object to the current thread. Therefore, every Web Form needs to derive from this BasePage.

Let's start with the Global.asax file:

void Session_Start(object sender, EventArgs e) 
    //set english as default startup language
    Session["MyCulture"] = "en-GB";

Alternatively, the culture can be defined in the Web.config file with the key <globalization culture="en-GB" /> and then be processed and stored in the Session object from the Session_Start method.

The next step is the master page:

<%@ Master Language="C#" AutoEventWireup="true" 
           CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

The buttons to change the culture can be either placed in the MasterPage directly, or in any embedded UserControl. In order to determine the requested language, the CommandArgument attribute of the LinkButton is used.

..And the code-behind of the master page:

public partial class MasterPage : System.Web.UI.MasterPage
    protected void Page_Load(object sender, EventArgs e)

    protected void RequestLanguageChange_Click(object sender, EventArgs e)
        LinkButton senderLink = sender as LinkButton;

        //store requested language as new culture in the session
        Session["MyCulture"] = senderLink.CommandArgument;

        //reload last requested page with new culture

The requested language, passed within the CommandArgument, is processed and stored in the Session object. Afterwards, the initially requested page will be reloaded on the server side.

Last but not least, the BasePage:

/// <summary>
/// Custom base page used for all web forms.
/// </summary>
public class BasePage : Page
    private const string m_DefaultCulture = "en-GB";
    protected override void InitializeCulture()
        //retrieve culture information from session
        string culture = Convert.ToString(Session["MyCulture"]);

        //check whether a culture is stored in the session
        if (!string.IsNullOrEmpty(culture)) Culture = culture;
        else Culture = m_DefaultCulture;

        //set culture to current thread
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

        //call base class

As mentioned above, the InitializeCulture method is overridden, and gets the stored culture from the Session object and assigns it to the currently running thread.

Remark: In this article, only the culture was mentioned. Of course, there is also the UI culture. But it is not of any further interest in this article since the handling is absolutely identical. For more information, please see the MSDN pages. :)

For a running example, download the Zip file above.


  • 11th May 2007 - First version released.
  • 16th November 2014 - Namespace corrected


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Ulmann
Architect Swissworx
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Swissworx
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Santhk25-Sep-15 23:29
professionalSanthk25-Sep-15 23:29 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun16-Nov-14 19:02
memberHumayun Kabir Mamun16-Nov-14 19:02 
GeneralRe: My vote of 5 Pin
Michael Ulmann16-Nov-14 19:17
memberMichael Ulmann16-Nov-14 19:17 
QuestionNot working when using standard controls of in default page Pin
vipz0077-Aug-13 20:30
membervipz0077-Aug-13 20:30 
QuestionChange sitemap menu language from LocalResources or GlobalResources Pin
Member 310338317-Apr-13 13:15
memberMember 310338317-Apr-13 13:15 
Questionadding Url Rewrite or routing Pin
deching11-Sep-12 1:08
memberdeching11-Sep-12 1:08 
GeneralThanks Pin
richihu14-Aug-12 3:01
memberrichihu14-Aug-12 3:01 
QuestionWebsite is localized, but how to I put querystrings on them to show different URLs? Pin
Jason P. Weber28-Jan-12 20:36
memberJason P. Weber28-Jan-12 20:36 
QuestionThank you for providing such clear instructions and offering a downloadable source code Pin
Jason P. Weber23-Jan-12 8:13
memberJason P. Weber23-Jan-12 8:13 
QuestionPS - Beautiful implementation Pin
fitITOren6-Sep-11 5:30
memberfitITOren6-Sep-11 5:30 
AnswerRe: PS - Beautiful implementation Pin
Michael Ulmann15-Nov-14 12:48
memberMichael Ulmann15-Nov-14 12:48 
SuggestionWhy session and not cookies?? Pin
fitITOren6-Sep-11 5:29
memberfitITOren6-Sep-11 5:29 
GeneralRe: Why session and not cookies?? Pin
Michael Ulmann10-Oct-11 18:41
memberMichael Ulmann10-Oct-11 18:41 
QuestionSuper great - what about using cookies for remembering for next time...? Pin
fitITOren6-Sep-11 5:22
memberfitITOren6-Sep-11 5:22 
AnswerRe: Super great - what about using cookies for remembering for next time...? Pin
Michael Ulmann15-Nov-14 12:49
memberMichael Ulmann15-Nov-14 12:49 
Questionweb.config Pin
Suici24-Jun-11 4:47
memberSuici24-Jun-11 4:47 
AnswerRe: web.config Pin
Michael Ulmann11-Aug-11 18:19
memberMichael Ulmann11-Aug-11 18:19 
GeneralLocalization Page(have issues) Pin
Kleen201113-May-11 11:16
memberKleen201113-May-11 11:16 
GeneralRe: Localization Page(have issues) Pin
Michael Ulmann11-Aug-11 18:23
memberMichael Ulmann11-Aug-11 18:23 
GeneralMy vote of 5 Pin
Omar Gamil26-Dec-10 0:38
memberOmar Gamil26-Dec-10 0:38 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 12:50
memberMichael Ulmann15-Nov-14 12:50 
GeneralMy vote of 5 Pin
kimic4-Nov-10 14:59
memberkimic4-Nov-10 14:59 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 12:50
memberMichael Ulmann15-Nov-14 12:50 
GeneralMy vote of 5 Pin
gico13-Oct-10 22:09
membergico13-Oct-10 22:09 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 12:50
memberMichael Ulmann15-Nov-14 12:50 
GeneralMy vote of 5 Pin
Keith Barrow21-Sep-10 9:00
memberKeith Barrow21-Sep-10 9:00 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 12:50
memberMichael Ulmann15-Nov-14 12:50 
GeneralGreat work! Pin
jonrandahl15-Sep-09 3:20
memberjonrandahl15-Sep-09 3:20 
GeneralMerci vielmal ;-) Pin
Darkjo12-Aug-09 22:24
memberDarkjo12-Aug-09 22:24 
GeneralRe: Merci vielmal ;-) Pin
Michael Ulmann11-Aug-11 18:22
memberMichael Ulmann11-Aug-11 18:22 
Questionerror in localization using masterpage Pin
mdazeemuddin31-May-09 2:53
membermdazeemuddin31-May-09 2:53 
AnswerRe: error in localization using masterpage Pin
Michael Ulmann31-May-09 3:06
memberMichael Ulmann31-May-09 3:06 
GeneralRe: error in localization using masterpage Pin
mdazeemuddin31-May-09 5:37
membermdazeemuddin31-May-09 5:37 
thanks for your support

you understand what i want to say.ya my all pages derive from the standard .NET System.Web.UI.Page class except OrderOnline Page which is derives from the base page

i will try to tell clearly what i want say is i run my application it open Default.aspx which is my startup page and this page is derive from standard .NET System.web.UI.Page class. If i select language as Arabic its perfectly translating my application into Arabic. If i open OrderOnline page which is derive from BasePage class
and i try to change my application language either English to Arabic or Arabic to English its simply submitting the OrderOnline form instead of doing translation.

why it is happening i don't know but i try lot to remove this bug but couldn't

so please help me where i am doing mistake.

thanks for your support
GeneralRe: error in localization using masterpage Pin
Michael Ulmann3-Jun-09 17:05
memberMichael Ulmann3-Jun-09 17:05 
GeneralTerrible Code! Pin
TV Mogul26-Apr-09 6:44
memberTV Mogul26-Apr-09 6:44 
GeneralRe: Terrible Code! Pin
Michael Ulmann26-Apr-09 15:41
memberMichael Ulmann26-Apr-09 15:41 
GeneralRe: Terrible Code! Pin
TV Mogul27-Apr-09 1:30
memberTV Mogul27-Apr-09 1:30 
GeneralRe: Terrible Code! Pin
TV Mogul27-Apr-09 1:49
memberTV Mogul27-Apr-09 1:49 
GeneralVery nice article Pin
Ezz Khayyat24-Dec-08 2:20
memberEzz Khayyat24-Dec-08 2:20 
GeneralRe: Very nice article Pin
Ezz Khayyat24-Dec-08 13:14
memberEzz Khayyat24-Dec-08 13:14 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 22:34
memberAlexander Nesterenko17-Dec-08 22:34 
Generalgreat article Pin
Simon Griffiths3-Dec-08 2:49
memberSimon Griffiths3-Dec-08 2:49 
GeneralRe: great article Pin
Michael Ulmann15-Nov-14 12:52
memberMichael Ulmann15-Nov-14 12:52 
Generalresource tags in master page Pin
daniel.zolnjan23-Oct-08 9:26
memberdaniel.zolnjan23-Oct-08 9:26 
GeneralThanks but I have a problem... Pin
swissroll5521-Oct-08 5:54
memberswissroll5521-Oct-08 5:54 
GeneralRe: Thanks but I have a problem... Pin
swissroll5521-Oct-08 5:58
memberswissroll5521-Oct-08 5:58 
GeneralAwesome Pin
Suresh2712928-Aug-08 6:52
memberSuresh2712928-Aug-08 6:52 
GeneralRe: Awesome Pin
Michael Ulmann15-Nov-14 12:52
memberMichael Ulmann15-Nov-14 12:52 
GeneralGreat! Pin
Shevchenko725-Aug-08 13:44
memberShevchenko725-Aug-08 13:44 
GeneralReally helpful but... Pin
famous29-May-08 1:49
memberfamous29-May-08 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Nov 2014
Article Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid