3

In my master page (.aspx file) I have this <div> to display the page title:

<div class="header">
<h1> PAGE TITLE GOES HERE </h1>
</div>

I also have a Web.sitemap file that holds the details of some pages (not all of pages in my applications)

What should I put in between <h1></h1> to let the master page dynamically display page title of current page and also if the current page is not in sitemap or doesnt have title then put some default string like "DEFAULT TITLE"

Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

1 Answers1

4

If you want to be lazy about it and don't care about code in your .aspx file, simply:

<h1><%= SiteMap.CurrentNode != null ? SiteMap.CurrentNode.Title : "not in sitemap!" %></h1>
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • Yup I am supper lazy and dont want to put a control in between

    tag then use if else at code behind. Thank you! I tried to put if else in between <% %> tag and got messed up, this method is much more clear!
    – Ronaldinho Learn Coding Feb 18 '14 at 17:00
  • Is the "not lazy" way of doing basically just using `<%: MasterTitle %>` in the aspx, then in the code behind having `MasterTitle { get { SiteMap.CurrentNode != null ? ... } }` – Nol Feb 07 '17 at 23:53
  • @Nol yes that is one way to do it. Or use a server tag... just keeps front-end a little cleaner. – MikeSmithDev Feb 08 '17 at 01:43