Jalle
Jalle

Reputation: 1622

ASP.NET can't set page title from content page

I have this page

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Charts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Page.Header.Title = "Some title";
    }
}

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <ul>
        <li><a href="ReportMissionType.aspx">ReportMissionType</a></li>
    </ul>
</asp:Content>

and in MasterPage.master there is title tag defined

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Title</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
<meta name="keywords" content="" />
<meta name="description" content="" />

</head>

but still when I try to set title from content page (Above) Page.Header.Title = "Some title"; it gives me error which says that Page.Header.Title Object reference not set to an instance of an object.

why can't i set title from the content page ?

Upvotes: 2

Views: 2483

Answers (2)

David
David

Reputation: 219047

According to the documentation, Page.Header:

Gets the document header for the page if the head element is defined with a runat=server in the page declaration.

In order to have elements accessible server-side as controls in WebForms, they need to run at the server:

<head runat="server">

Upvotes: 1

PHeiberg
PHeiberg

Reputation: 29851

In order to access children of the head element, you need to set the runat="server" attribute on the head element.

<head runat="server">
  <title>Title</title>
 ...
</head>

Upvotes: 5

Related Questions