Change title text dynamically
So in the past I have changed the title text of my pages from within the code. Not bad just more server side code than probably really needed to be. As I am working on recreating a site and wanting to make sure the titles of each page we unique to that page, plus forgetting how I had made these changes in the past, I did a quick search and ran across this article. These are pretty cool options that I think keep your server side code less muddy and you get the same results. I should mention that I am trying to work towards building sites in MVC so to see this used in MVC coding makes me want to learn more about MVC.
Here is exactly what I used.
On the master page you insert a ContentPlaceHolder and a Literal control inside the title tag:
1 2 3 4 | <title> <asp:Literal runat="server" Text="Reverse Phone Lookup "></asp:Literal> <asp:ContentPlaceHolder ID="titleText" runat="server"></asp:ContentPlaceHolder> </title> |
Then on each of the following pages you add your Content control with the title information:
1 2 | <asp:Content ID="Content3" ContentPlaceHolderID="titleText" runat="server"> | Reverse Cell Phone Lookup </asp:Content> |
The Literal control is added if you want to make sure that each page has some consistent text. Then the Content control adds on whatever from the new page.
This is pretty cool and helpful way to change title text dynamically as it keeps this type of information in the aspx page and not in your code. Just one more thing to keep content where it should be.