Posted  21 Nov 2008 - 20:20:51


Setting Page title in master page from controls.

Sometimes you wish to set the title of the page based on what content you have placed on it. Say you have a blog and you want the title of this post to end up in the page title. You probably use masterpages.

First off, set the main title in the Masterpage to something like this:

<title id="title" runat="server"></title>

Then, from any page, set the page title like this:

((HtmlGenericControl)Page.Master.FindControl("title")).InnerText = ((Label)sender).Text;

I realise this is a bit dirty since FindControl might not always find controls, based on different conditions. In this cause you're pretty safe, and it's more to give you an idea how to access the title. On anything more than an experiment, you should add checks if the control was actually found etc.


© SolutionCottage 2008-2011 7/26/2017 1:44:02 AM