views:

144

answers:

5

To manage page title on page's,I have a master page where i am taking ContentPlaceHolder.

  <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />  </title>

and on every page i write

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Some Title Here
</asp:Content>

Now my client ask me for remove title on all page's and keep it on master page but not remove content place holder code on all page's and master page so that in future if any requirement then we can insert data in to them. So my problem is without removing them on master page and pages i am not able to put title on master page.So how can i handle this situation?

A: 

Use the OnPreRender event on the master page to set the title, overriding what has been set on each page.

Greco
It's MVC, better avoid the whole page events altogether.
Jan Jongboom
Oh well, he wants minimal changes and this is then the way to go..
Greco
A: 

Easiest way:

Move the current ContentPlaceHolder somewhere to your HTML, and wrap it in a <asp:PlaceHolder runat="server" visible="false"/>. When you'll be needing it later on, just move the ContentPlaceHolder back again.

Jan Jongboom
i can't move it from title,but i set Visible="false"<title>My Title<asp:ContentPlaceHolder ID="TitleContent" runat="server" Visible="false" /> </title>, still not working
Pankaj
You need to create a new `<asp:PlaceHolder>`, like `<asp:PlaceHolder runat='server' Visible='false'><asp:ContentPlaceHolder ... /></asp:PlaceHolder>`
Jan Jongboom
A: 

why not add attribute Visible=false to ContentPlaceHolder of Master Page

I think this is the easiest way to handle your situation.

Happy coding.

Ravia
this is not working. i did on this way <title>My Title<asp:ContentPlaceHolder ID="TitleContent" runat="server" Visible="false" /> </title>and on all pages<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> </asp:Content>but still it is not working fine
Pankaj
A: 

Thanks Guys.. I got solution

if you want to set part of the title from within the master page. For example, you might want the title of every page to end with a suffix, “ – MySite”.

If you try this (notice the – MySite tacked on):

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:ContentPlaceHolder ID="titleContent" runat="server" /> - MySite
  </title>
</head>

And run the page, you’ll find that the – MySite is not rendered. This appears to be a quirk of the HtmlHead control. This is because the title tag within the HtmlHead control is now itself a control.

The fix is pretty simple. Add your text to a LiteralControl like so.

<%@ Master ... %>
<html>
<head runat="server">
  <title>
    <asp:ContentPlaceHolder ID="titleContent" runat="server" /> 
    <asp:Literalrunat="server" Text=" - MySite" />
  </title>
</head>
Pankaj
Fyi, this is generally a BAD idea for SEO compliancy/ranking. Your TITLE should match your first H1 exactly, else Google and Bing may report errors. http://eduncan911.com/blog/8-steps-to-seo-quickly.aspx
eduncan911
we don't need SCO.
Pankaj
+1  A: 

If you want a good solution to overriding the page title:

Create a class of your own that inherits from the System.Web.Mvc.ViewPage.

Have your view pages inherit from that class:

Write a Page_Load handler in your new class that does something like this:

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
   Me.Title = "Company Name | " + Page.Title
End Sub

You also don't need a content place holder to change the title. The <head> tag is already a runat server control. Setting the Page.Title in the page load (or earlier event) work just fine.

You could also put a runat server script tag in your master page to accomplish this task too.

Jeremy Bell