tags:

views:

87

answers:

2

There are different ways to set the title for content pages from Master page

  1. by findcontrol
  2. by creating property in master page and setting the value in content page

As both method requires an object creation of master page which will be little heavy
myMasterPage myMaster = (myMasterPage)this.Master;

so I have tried it by creating a class and it worked -

public class clsmaster
{
    public static clsmaster objmaster = new clsmaster();
    public strtitle {get;set;}
}

Now I just need to access this static object and set the property in the content page and in the master page I just need the controls to take the value from this class (clsmaster).

I would like to know which one is the better approach and why with description please?

A: 

See this blog post I wrote for one suggestion on a way to handle this problem.

Wyatt Barnett
Hi Wyatt,Thanks for ur reply. would like to know which one is the better approach and why
Mahesh
A: 

I generally advise creating a BasePage class of some sort that encapsulates the behavior you want through all of your pages, these objects are assumed to always have the same master page, if you need other setups then you can create other objects as necessary.

From there you can create some properties or methods to allow the BasePage objects to access the master page or its associated properties in a very easy to code way such as this.Title = "MyTitle"

You can get fancier and create some virtual methods on your BasePage class that you can then override on the physical pages as necessary, to set titles, etc, as needed, without ever grabbing the Master page object directly (which yes, is annoying and ugly). This might look something like this.Title = GetTitle(); (GetTitle is a virtual method on the BasePage that is overridden in the child pages as needed for those pages you want to set a title for).

This makes strong use of inheritance and lets you add functionality to all of your pages very easily, it may be overkill for what your doing but I've never found a situation that was too simplistic for this architecture, it just works really well. I personally find this design far better than constantly using FindControl(), which tends to be error-prone when control ID's change, etc.

  1. FindControl() is bad because if the control ID's change, then you might forget to update them in the FindControl reference, and now it'll break the next time its executed, I stay well away from static stuff like this if at all possible for this very reason, it's a cheap, quick but error-prone solution.

  2. Accessing the Master page directly isn't inherently bad, I just can't stand stuff like:

    myMasterPage myMaster = (myMasterPage)this.Master

Gets old, is uglier than it needs to be, wrap it in an accessor property at the very least ;)

I like the following better:

Title = "My Title"; // Property

or

Title = GetTitle(); // Virtual method
Capital G
Hi Capital G, Thanks For ur Reply, so u mean to say that I should create a base page with properties and set the properties in the appropriate pages and then My MasterPage will access the properties of the basepage and set the value for the appropriate controls in the master page.Will this not create any problem when muliple instances of the same page will existspls clariy me with it.
Mahesh
Sorry in getting to you so late, I was on vacation. I'm not sure what you mean by multiple instances of the same page. If you mean many pages will have the same master page, no, it's not a problem as long as all your pages inherit from the BasePage, because then they are all talking to the same Master page. If you need multiple master pages then you can simply make multiple versions of your BasePage like 'SiteBasePage' or 'AdminBasePage' or whatever you need. Also none of this is automatic, you will have to hook up the architecture between base page and master page, but after that its easy.
Capital G