Andy Stevens
Andy Stevens

Reputation: 25

Dynamically set master page body title

Similar to the question below, I am trying to grab the Title entered in the title tag in each page and use that to update an asp label to dynamically set the title on each page. I am trying only use the c# code on the master page, yet only empty strings are being returned by page.title when each page has a title such as:

<title>Graphs</title>

Get Page Title In Master Page Code Behind

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      string TempTitle = Page.Title;
        title.Text = TempTitle;
    }
}

Ok it seems 'Page.Title;' doesn't grab the text between the title tags, does anything?

Upvotes: 0

Views: 381

Answers (2)

Andy Stevens
Andy Stevens

Reputation: 25

Problem was the asp title was empty as shown below

<%@ Page Title=""

Upvotes: 0

Shaiwal Tripathi
Shaiwal Tripathi

Reputation: 627

It doesn't make any sense to code in master page if you want to get title of each page in c#. Just use this code on each page.

string S = this.Page.Title;
YourLabel.Text = S;

Upvotes: 0

Related Questions