Saturday, February 07, 2009

Master Pages in asp.net Content Page Title not Changing a beginners nightmare!!!

If you are trying to change the title of your content page and its not changing and always showing "Untitled Page"  then you are probably committing a stupid mistake.

Here i will tell you what mistake are you doing and how to rectify it.

If you are working with old asp.net versions then it will not insert the content place holder in the head section of the master page.It will only insert the content place holder in the body section of the master page.

Also by default there will be a title entry in the master page <head> section, this is the entry which will always be shown no matter whatever title you give to your content page.

So the solution to this problem is follow the steps given below:-

  • Make sure that there is a content place holder defined in the head section of the master page
      <%@ Master Language="C#" AutoEventWireup="true" CodeFile="CatalogueMain.master.cs"
          Inherits="CatalogueMain" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          
          <script src="http://www.google.com/jsapi"></script>
           <script type="text/javascript">
               google.load("jquery", "1.3.1");
           </script>
           
          <script type="text/javascript" src="Scripts/jquery.popupcustom.js"></script>
      
          <script type="text/javascript" src="Scripts/jquery-1.3.1.js"></script>
      
          <script type="text/javascript" src="Scripts/jquery-jtemplates.js"></script>
      
          <asp:ContentPlaceHolder ID="head" runat="server">
          </asp:ContentPlaceHolder>
      </head>
  • Here we have a ContentPlaceHolder having an id of "head" defined
  • Next make sure that you delete the <title></title> from the head section of your masterpage.
  • Thirdly make sure that while creating  a content page in the extreme right of the bottom of the selection wizard you select "Select a masterpage".
  • This will automatically create two content place holders for you if you already have put those two in your original master page.
  • So now in the content place holder on the content page which in our case will be having ContentPlaceHolderID as "head" is the place where we will put our <title></title>
    • <%@ Page Title="" Language="C#" MasterPageFile="~/CatalogueMain.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
      
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
      <title>2Bok Home</title>
      </asp:Content>
  • So now go ahead and put the title as anything .......And that's it run your content page and you will see the title applied.

This was a very simple workaround and was meant only for beginners who sometimes get confused.

Update:- Second Method which is even Simpler

Thanks to Lee as i forgot to mention this method.

Just in the content page in the @ Page Directive in the Title Section provide a valid Title and this will become the title for your content page.

6 comments :

Anonymous said...

It was very helpful.

Thank you

Lee said...

You are apparently unaware of the Title attribute of the @Page directive for content pages.

Aashish said...

@Lee
Thanks Lee ya i forgot to mention that and now i have updated the post.
Thanks again for your valuable suggestion

Mavabe said...

Lee's method is better, as with the first method, you may end up with two <title> tags in the header: one coming from the content page, and one from the (empty) @Page directive.

Validators don't like two <title> tags in the header.

Chris Smith said...

Here's yet another way of doing it, binding data to the page's title:
Setting the page title dynamically in ASP.NET

Dhananjay said...

i am unaware of the Title attribute of the @Page directive for content pages. This help me a lot thanks.....Djay

Post a Comment