viv_acious
viv_acious

Reputation: 2489

How to change each page header?

This might seem like a silly question, but I am using VS 2010 and in the Site.Master page, there's a tag <h1> which allows users to type in the title header.

E.g. enter image description here

In this case, the title is 'ASP.NET Application'

I'd like to have a different title in every page of my application.

How do I go about updating the title in each of my .aspx page? In the HeaderContent section of the page?

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

Upvotes: 1

Views: 11801

Answers (4)

Raj Baral
Raj Baral

Reputation: 671

If looking to pass some text from each .aspx page to master page,try following. I had the similar situation and I resolve it as:

Place a label control with ID="lblOnMasterPage" on the master page.

<asp:Label ID="lblOnMasterPage" runat="server"></asp:Label>

On code-behind file of the master page create a public property as

        public string LabelValue
        {
            get { return this.lblOnMasterPage.Text; }
            set { this.lblOnMasterPage.Text = value; }
        }

and use following code to pass the title/text you want for every page of your application..

((yourMasterPage)this.Master).LabelValue = "text you want to pass from each page";

Upvotes: 0

ajakblackgoat
ajakblackgoat

Reputation: 2149

Inside the Site.Master page, change the following block

<h1>
     My ASP.NET Application
 </h1>

to

<h1><%= Page.Title%></h1>

Then, in each content page, set the page title in the page directive:

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeBehind="Default.aspx.vb" Inherits="WebApplicationTest._Default" %>
  • Note the Title="Home Page" attribute.

Or from code-behind:

Page.Title = "Home Page"

Upvotes: 2

MikeSmithDev
MikeSmithDev

Reputation: 15797

What you want to do is different from the intent of the template. The template was constructed so that the contents of the <h1> would be your site logo or site name. That is why they hard-coded it into the Site.Master as:

<div class="title">
    <h1>
        My ASP.NET Application
    </h1>
</div>

It wasn't meant to be changed per page.

If you want to change it per page, then you have a couple of options. Here is one.

Since you referenced the header section of the site master: lets say you want the text of the title set to the actual page title. You could do it like this:

<div class="title">
    <h1>
        <asp:Label ID="_pageTitle" runat="server"></asp:Label>
    </h1>
</div>

So you replace My ASP.NET Application with a label so you can easily change it in the code behind.

Then, in your code behind, you have something like:

protected void Page_Load(object sender, EventArgs e)
{
    _pageTitle.Text = Page.Title;
    //rest of your  code
}

This will set the text of the label to the page title of the page.

Upvotes: 2

Nathan
Nathan

Reputation: 25006

All you need to do is update the header in the master page and then every single web page that uses that master page will automatically have the header updated

Upvotes: 1

Related Questions