Duplicate Header Tags Using ASP.NET MasterPage

I need to set the page title dynamically, so I am using code similar to the following:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="~/about.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<%@ MasterType VirtualPath="MasterPage.master" %>
<%@ OutputCache Duration="43200" VaryByParam="*" Location="Server" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <title><%=pageTitle%></title>
</asp:Content>

But this creates duplicate header tags. Is there any way around this? Thanks.

EDIT: Following the suggestions below, I now have the following in my MasterPage:

<head id="Head1" runat="server">
<title>Default Title</title>
...
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head> 

and on my main page:

    this.Title="xxx";

but I get no name (neither "Default Header" nor "xxx").

EDIT: Nevermind. Using this method, it worked.

+5
source share
8 answers

Your .master header should look like this:

<head runat="server">
<title>Default Title</title>
  .. other tags
</head>

Then in the page code in Page_Load you write:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "My Page Title";
}
+8
source

head, , Head ( runat="server") Title , , .

, -

<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>

-

  • runat="server" , , .
  • , .

, ContentPlaceholder, . ( , )

Phil Haack .

+10

, : .

MasterPage - :

<head>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

title= "Your Title" :

<%@ Page Title="Your Title" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

, .

, .

+5

        ((MyPageClass)Page).Title = "Your Page Title"

Page_Load . cast ( "MyPageClass" ), PageClass, . , .

, , :

     Title = "Your Page Title"

Page_Load.

0

:
  <title><%=someTitleVariable%></title>

:   <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <%=pageTitle%> </asp:Content>

0

<asp:Content ContentPlaceHolderID="head"> , , "head". , - :

<head>
  <title>Master Page Title</title>
  <asp:ContentPlaceHolder id="head" runat="Server" />
</head>

, <asp:ContentPlaceHolder> <asp:Content ContentPlaceHolderID="head">, <title>.

, <title> , , <title> aspx - - , - ...

this.Title = pageTitle;
// if that doesn't do it try
// this.Header.Title = pageTitle;
0

2 - . SEO .

Using MVC 4.5RC. All you have to do is put an empty tag in front of the main content area. There is no need to recode, set the header inside the code. For example:

<title></title>
<asp:contentPlaceHolder><title>Rise Sir...</title><asp:contentPlaceHolder>

Simple

0
source

I think using:

If you want to set the page level heading

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:ContentPlaceHolder ID="titleContent" runat="server" />
  </title>
</head>

Or

If you want to set a dynamic header at the home page level.

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:Literal ID="litPageTitle" runat="server"></asp:Literal>
  </title>
</head>

is the best way to ensure that an empty header tag is not created.

0
source

All Articles