ASP.Net MVC上的母版頁標題

[英]Title on Master Page on ASP.Net MVC


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

要管理頁面上的頁面標題,我有一個母版頁,我正在使用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?

現在我的客戶要求我刪除所有頁面上的標題並將其保留在母版頁上但不刪除所有頁面和母版頁上的內容占位符代碼,這樣以后如果有任何要求,我們就可以將數據插入到它們中。所以我的問題是沒有在母版頁和頁面上刪除它們我無法將標題放在母版頁上。所以我該如何處理這種情況?

5 个解决方案

#1


2  

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”.

如果要在母版頁中設置標題的一部分。例如,您可能希望每個頁面的標題以后綴“ - MySite”結尾。

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

如果你試試這個(注意 - MySite加上):

<%@ 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.

並運行該頁面,您會發現 - MySite未呈現。這似乎是HtmlHead控件的一個怪癖。這是因為HtmlHead控件中的title標簽現在本身就是一個控件。

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

修復非常簡單。像這樣將文本添加到LiteralControl。

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

#2


1  

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.

創建一個繼承自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:

在新類中編寫一個Page_Load處理程序,它執行以下操作:

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.

您也不需要內容占位符來更改標題。 標記已經是runat服務器控件。在頁面加載(或更早的事件)中設置Page.Title工作正常。

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

您還可以在母版頁中放置一個runat服務器腳本標記來完成此任務。

#3


0  

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.

將當前的ContentPlaceHolder移動到HTML,並將其包裝在 中。如果您稍后需要它,只需再次移回ContentPlaceHolder。

#4


-1  

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

使用母版頁上的OnPreRender事件設置標題,覆蓋每頁上設置的內容。

#5


-1  

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

為什么不將屬性Visible = false添加到母版頁的ContentPlaceHolder

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

我認為這是處理您情況的最簡單方法。

Happy coding.


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/01/11/72596ebb7c6b0ca95f1e753a3099fd32.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com