viv_acious viv_acious - 4 years ago 81
ASP.NET (C#) Question

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>

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download