Maintain Constant Title Across Website
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
L开发者_如何学JAVAinks 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).
This may answer your question: 4guysfromrolla link
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")
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).
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; };
}}
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!
精彩评论