Warning: this is a geek posting. Every so often, you'll find them here. I am a programmer by trade and a cook by passion.Situation:You are using ASP.NET and have Masterpages. You want to display the title for your content pages, but there is no <title> tag on the content pages. Instead, you notice there is a title attribute on the @Page directive. How does this work? How can you take advantage of it?
Follow this set up to display the value from the title attribute. I added a few advanced concepts that adds a degree of professionalism.
Masterpage HTML Source<head id="headPage" runat="server">
<title></title>
</head>Masterpage codebehind (VB.NET)Protected Sub Page_Load(
ByVal sender
As Object, ByVal e
As System.EventArgs)
Handles Me.Load
headPage.Title = HttpUtility.HtmlEncode(headPage.Title)
End SubContent page<%@
Page ...
MasterPageFile="
~/MyMaster.Master"
title="
Recipe Details" %>
This is it. Try it out. There is nothing more you have to do. Keep reading if you want to see some more you can do.
Masterpage codebehind (VB.NET) Advanced Topic
Protected Sub Page_Load(
ByVal sender
As Object, ByVal e
As System.EventArgs)
Handles Me.Load
headPage.Title = String.Format("
{0}{1}{2}",
My.Settings.WebPageTitlePrefix, HttpUtility.HtmlEncode(headPage.Title),
My.Settings.WebPageTitleSuffix)
End Sub
Here is the idea. Have you noticed professional sites have their name on every page's title? If you visit
Microsoft.com's Office pages, then you'll see "Microsoft Office Online" in every title. Use the above code to do the same thing.
You either need to create the two settings I have listed or replace those two settings (
WebPageTitlePrefix and
WebPageTitleSuffix) with literal string values. I like the settings because the value for My.Settings are stored in the web.config. This technique allows me to change the value without having to compile the application and deploy it. I can simply access the web.config and edit the values.