在整个网站上保持不变的标题

发布于 2024-09-02 07:17:54 字数 329 浏览 3 评论 0原文

如果我在 ASP.NET 中创建一个网站,是否可以通过编程方式将页面标题设置为某个预定义值并附加一些额外信息?例如:

 Home Page   Title = Site Name
 Links       Title = Site Name: Links
 Stuff       Title = Site Name: Stuff

基本上,无论我在当前所在页面上定义什么主标题,我都想将“:名称”添加到标题的末尾,以便它在整个网站上保持一致。我正在考虑将其定义为 ContentPlaceHolder 并围绕它包装一些逻辑,但它似乎并没有像我想象的那样工作(又名,根本没有)。

If I am creating a website in ASP.NET, is it possible to programmatically set the title of the page be some predefined value with some extra information tacked on? For example:

 Home Page   Title = Site Name
 Links       Title = Site Name: Links
 Stuff       Title = Site Name: Stuff

Basically, whatever I defined as the main title on the page I'm currently on, I want to tack ": Name" onto the end of the title so it stays consistent across the website. I was thinking of defining it as a ContentPlaceHolder and wrapping some logic around it, but it doesn't appear to work how I thought it would (AKA, not at all).

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

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

发布评论

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

评论(5

伏妖词 2024-09-09 07:17:54

这可能会回答你的问题:
4guysfromrolla 链接

This may answer your question:
4guysfromrolla link

十二 2024-09-09 07:17:54

在您的母版页中尝试此操作

protected void Page_Load(object sender, EventArgs e)
{
PreRender += new EventHandler(MasterPage_PreRender);
}

void MasterPage_PreRender(object sender, EventArgs e)
{
Page.Title = "Site Name - " + Page.Title;
}

,并将页面标题放入内容页面的 @Page 指令中(Title="Blah" 使其成为“站点名称 - Blah”)

try this in your master page

protected void Page_Load(object sender, EventArgs e)
{
PreRender += new EventHandler(MasterPage_PreRender);
}

void MasterPage_PreRender(object sender, EventArgs e)
{
Page.Title = "Site Name - " + Page.Title;
}

and put the pages title in the @Page directive of the content page (Title="Blah" to make it "Site Name - Blah")

雨落□心尘 2024-09-09 07:17:54

Expression Web 具有动态 Web 模板(目前我最喜欢的)和母版页。 DWT 非常易于使用,并且完全可以满足您的需求,真正节省时间。您为整个站点创建一个 DWT(模板页面),然后在该页面中包含可编辑区域,可以对这些区域进行编辑以使所有其他页面都独一无二。此外,Expression Web 与其他 MS 产品和功能(如 Visual Studio 和 ASP.NET)配合得很好。

Expression Web has both Dynamic Web Templates (currently my favorite) and Master Pages. DWT is very easy to use and does exactly what you are looking for, a real time saver. You make one DWT (the template page) for your entire site, then in that page are editable regions that can be edited to make all of your other pages unique. Additionally, Expression Web works great with other MS products and features (like Visual Studio and ASP.NET).

荒路情人 2024-09-09 07:17:54

如果您使用的是母版页,Lerxst 答案应该可以做到,如果没有,您可以使用 BasePage 来完成您想要的操作。像这样的事情:

public abstract class BasePage : Page
{
  protected abstract string Subtitle { get; }
  protected BasePage()
  {
    Page.Load += (s, e) => { Title = "Site Name: " + Subtitle; };
  }}

If you are using a master page Lerxst answer should do it, if not you can have a BasePage to acomplish what you want. Something like this:

public abstract class BasePage : Page
{
  protected abstract string Subtitle { get; }
  protected BasePage()
  {
    Page.Load += (s, e) => { Title = "Site Name: " + Subtitle; };
  }}
你是暖光i 2024-09-09 07:17:54

虽然 Lerkst 的答案适用于 ASP.NET Webforms,但我正在尝试通过 ASP.NET MVC 来做到这一点。因此,Site.Master 没有代码隐藏页面(因为不应该有)。因此,在做了一些研究之后,我遇到了 tGuillaume Roy 的帖子 讨论了如何使用 ActionFilterAttribute 在数据设置中利用控制器(并且视图是“哑的”并且仅呈现它)。这允许我将数据插入视图,从而完成我想做的事情。

希望这可以帮助其他人!

While Lerkst's answer would work for ASP.NET Webforms, I am trying to do this through ASP.NET MVC. As a result, there is no code-behind page for the Site.Master (as there should not be). So, after doing a bit of research, I ran across this post by Guillaume Roy which discusses how to use an ActionFilterAttribute to utilize the Controller in the setting of that data (and the View is "dumb" and only renders it). This allows me to insert data into the view and, thus, accomplishing what I wanted to do.

Hopefully this helps someone else out!

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