Search code examples
c#asp.netlocalizationuiculture

ASP.NET Templating with BasePage vs MasterPage


I have to deal with an ASP.NET project in which i have to support localization and translation. Change of colture may happen only in InitializeCulture method of Page object and not on MasterPage.

Usually when i have to deal with common elements (header/footer) i put them in MasterPage but doing so prevent me to properly localize those elements. So i am intereseted in understand how to template a page using a common base page that will be derived by all other pages (a sort of MasterPage).

I have found this article that explain clearly how to handle such situation, but i think is a bit outdated : https://www.codeproject.com/Articles/2915/ASP-NET-Page-Templates-Using-Inheritance

So i would ask if there is a better approach or technology to achieve my goal.


Solution

  • Here a simple demo of how to send a variable to the Master Page. Put a public property in the Master named Language.

    public partial class Site1 : System.Web.UI.MasterPage
    {
        public string Language { get; set; }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = $"My language is {Language}.";
        }
    }
    

    Then add a property for the Master in the normal page and cast Page.Master to your Master Page's partial class (Site1 by default).

    public partial class Default1 : System.Web.UI.Page
    {
        public Site1 master;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            master = (Site1)Page.Master;
        }
    

    Then you can access Language in the Master from the Page.

    master.Language = "nl-NL";