Set Page Title on asp content where it uses master page
Asked Answered
U

4

6

Ok this an odd way of doing this, I know I should have done it on Page_Load in every pages when using Masterpage, but is there a way around this issue? I don't want to rebuild the pages if I can help it,if I can only insert a title on <asp:content></asp:content> it would be easier.

If anyone has run into this or maybe have a suggestion of a good way to do it , I know jquery can do it document.title ='' but I heard it's not SEO friendly.

Thanks!

Unsubstantial answered 7/2, 2014 at 1:53 Comment(1)
Nothing can be done through the Content control; only from code-behind.Urson
G
16

You can still set the title in each page that's using a MasterPage. In markup: -

<%@ Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc

Or in code: -

protected override void OnLoad(EventArgs e)
{
    Page.Title = "Your Title";
    base.OnLoad(e);
}
Goldberg answered 7/2, 2014 at 2:43 Comment(1)
yes this is what I'm tryin to avoid coz I have to rebuild the pages. thanks thoUnsubstantial
L
9

you can place a contentplaceholer in your master page as..

<asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>

After that In content page add its reference as..

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title>  Your title goes here.  </title>
</asp:Content>
Levorotation answered 7/2, 2014 at 6:15 Comment(1)
omg! I'm excited this , I didn't know you could do this. I'll give you plus 20 if this works!Unsubstantial
D
8

more simple solution in Master page <%: Page.Title %> - the main title goes here

in content page first line of it <%@ Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc

Discus answered 21/9, 2015 at 0:34 Comment(0)
J
3

I know this is an older question but what definitely works for me is to set a Literal control in the title tag of the MasterPage like this:

<title><asp:Literal runat="server" id="pagetitle" Text="MyTitle"></asp:Literal></title>

Then, in the content page, put this in the Page_Load method:

Literal myTitleLiteral = (Literal)Master.FindControl("pagetitle");
    if (myTitleLiteral != null)
    {
        myTitleLiteral.Text = "Test Title"; 
    }
Junkojunkyard answered 1/5, 2018 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.