views:

82

answers:

5

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

+1  A: 

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

typoknig
+1  A: 

This may answer your question: 4guysfromrolla link

Thyamine
+1  A: 

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")

Lerxst
Maybe there is something else going wrong, then. I was (basically) doing this, but it doesn't seem to change the title of the page at all. Even when I put a breakpoint into the code behind (in the Page_Load event handler), it doesn't stop on loading. Any suggestions?
JasCav
Not sure whats wrong here, but try putting the line that's in PreRender into the Page_Load handler. In my tests I could put that line directly in the page_load and it worked.If you are using C#, the page_load handler should be already hooked up and added to the code file. Just stick that line in there.
Lerxst
@Lerxst - Well, that might be part of the problem. There was no code behind file for the Site.Master file, so I had to create it myself (Site.Master.cs) and extend Page and then create the above methods. I am using ASP.NET MVC, so I don't know if this would change anything. (I tried your suggestion and that didn't fix things.)
JasCav
Im not sure if that would cause a problem or not, can you paste your @Master declaration and your class definition from your code-behind? It sounds like they are not connecting...
Lerxst
A: 

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; };
  }}
alejandrobog
A: 

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!

JasCav