Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Tuesday, May 10

Change Master Pages (VS 2005) at run-time

Master Pages are one of the hottest new features of ASP.NET 2.0. You will love it! Developers can now create a website in which various physical pages share a common look and feel. The master page will contain ContentPlaceholders for content that the derived pages will provide. All common information is shared in a single master page. At design-time you can visually see how your aspx-page will eventually look like and you are only able to edit the ContentPlaceHolder-regions.

In my example I've used a master page to create a website that will support navigation for multiple languages (English - French - Dutch). It's not a typical example for using master pages, but it shows you how you can change your master page at run-time and support for multiple languages is almost always required for Belgian websites. Belgium has three official languages : French, Dutch and German ... but English is most of the time used for general communication in the bigger companies. English has become the "working" language in Belgium. Yip, strange behavior for a country with a population of only 10 million people!

The website contains only 1 aspx-page (Default.aspx) that is based on 1 master page (MyMasterPage.master). At the end, the page will be able to display content in English, French and Dutch and will disable the hyperlink for the current language.



The master page contains three "Hyperlink"-controls and a body-ContentPlaceHolder. All three links refer to the same aspx-page with a different Querystring.



The aspx-page is linked to the MyMasterPage and a heading 2 is entered into the ContentPlaceHolder-block.



The CodeFile for the master page contains a public method for disabling a hyperlink according to the language-parameter.



In the Page_Load method in the CodeFile for the aspx-page, the language is set (default = English), the "DisableLink" method is called and the Title + Header is properly set.



This is only a very simple example of how you can alter a master page at run-time, but it's very powerfull because it lets you change each server-control that's created in your master page. You can also work with properties on your master page instead of with public methods.

1 Comments:

  • At 10:52 AM, Anonymous Anonymous said…

    Interesting post.
    I have nested Master pages:
    1. MyMasterPage.master and 2. MySubMasterPage.master that inherits (1.)
    I have a method, Add(...) in MySubMasterPage which I want to reach from the content page (to MySubMasterPage)

    I tried to use:
    private MySubMasterPage mySubMasterPAge;

    protected void Page_Load(...)
    {
    this.mySubMasterPAge = (MySubMasterPage)this.Master.Master;

    this.mySubMasterPAge.Add(...);
    }

    But I get a runtime error.
    Unable to cast object of type 'ASP.mymasterpage_master' to type 'mySubMasterPAge'.

    Any idea´s of how I should do get a hold of an object of mySubMasterPAge so I can run the Add method.

    Regards
    /Jesper

     

Post a Comment

<< Home