ASP.Net MVC 母版页上的标题

发布于 2024-08-17 19:00:49 字数 471 浏览 3 评论 0原文

为了管理页面上的页面标题,我有一个母版页,我在其中使用 ContentPlaceHolder。

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

现在,

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

我的客户要求我删除所有页面上的标题并将其保留在母版页上,但不要删除所有页面和母版页上的内容占位符代码,以便将来如果有任何要求,我们可以将数据插入到他们。 所以我的问题是,如果不删除母版页上的它们,我就无法在母版页上放置标题。那么我该如何处理这种情况呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伊面 2024-08-24 19:00:49

谢谢大家..

如果您想从母版页中设置部分标题,我得到了解决方案。例如,您可能希望每个页面的标题都以后缀“-MySite”结尾。

如果您尝试此操作(请注意附加的 –MySite):

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

并运行该页面,您会发现 –MySite 未呈现。这似乎是 HtmlHead 控件的一个怪癖。这是因为 HtmlHead 控件中的标题标签现在本身就是一个控件。

修复方法非常简单。像这样将文本添加到 LiteralControl 中。

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

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>
要走就滚别墨迹 2024-08-24 19:00:49

如果您想要一个好的解决方案来覆盖页面标题:

创建一个您自己的继承自 System.Web.Mvc.ViewPage 的类。

让您的视图页面继承该类:

在新类中编写一个 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

您也不需要内容占位符来更改标题。 <头> tag 已经是一个 runat 服务器控件。在页面加载(或早期事件)中设置 Page.Title 效果很好。

您还可以在母版页中放置一个 runat 服务器脚本标记来完成此任务。

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.

琉璃梦幻 2024-08-24 19:00:49

最简单的方法:

将当前的 ContentPlaceHolder 移动到 HTML 中的某个位置,并将其包装在 中。当您稍后需要它时,只需再次将 ContentPlaceHolder 移回来即可。

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.

野却迷人 2024-08-24 19:00:49

使用母版页上的 OnPreRender 事件设置标题,覆盖每个页面上设置的内容。

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

攒眉千度 2024-08-24 19:00:49

为什么不将属性 Visible=false 添加到母版页的 ContentPlaceHolder

我认为这是处理您的情况的最简单方法。

快乐编码。

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

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

Happy coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文