Adding a header on every content page

Last post 10-08-2007 11:23 AM by jgd12345. 4 replies.

Sort Posts:

  • Adding a header on every content page

    10-06-2007, 11:45 AM
    • Member
      487 point Member
    • jgd12345
    • Member since 06-22-2006, 5:28 PM
    • Posts 267

    Hi, i have my own site map provider and every page has a master page.  I need to have a header (h1 tag) on every content page which is taken from the site map.  I have come up with a few ideas but all have there problems

    1. Place <h1><%# SiteMap.CurrentNode.Title %></h1> where i want the header in every page but this means i have to call this.DataBind() in the page load of every page i wish to have a header and this is something i don't like doing.

    2. Similar to above but putting a label within the h1 and setting it in the code behind but again means i have to add code to every page.

    3. Place the h1 in the master page page but this doesn't give me much control over where i wish to place the h1 on each page.

    4. Place a literal within the h1 on every page and set it from within the master page.  I could not get this to work.  I tried doing ((Literal)this.Page.FindControl("litH1")).Text = SiteMap.CurrentNode.Title within the page load event handler of my master page but this didn't seem to do anything.

    Option 4 Seems best but i can't get it to work.  I don't like the idea of adding code to my page load of every content page but if i could somehow set it in a base page then that would be good enough.

    Appreciate if someone could help.  Thanks

  • Re: Adding a header on every content page

    10-06-2007, 12:39 PM
    • Star
      13,175 point Star
    • SonuKapoor
    • Member since 04-11-2004, 12:24 PM
    • Montreal/Canada
    • Posts 2,652
    • TrustedFriends-MVPs

    Why don't simply you add the h1 into your master page and set it from the pageload of your master page? 

  • Re: Adding a header on every content page

    10-06-2007, 12:40 PM
    • Participant
      1,221 point Participant
    • PureWeen
    • Member since 04-09-2005, 5:44 PM
    • Posts 317

    http://www.simple-talk.com/dotnet/asp.net/asp.net-master-pages-tips-and-tricks/

    Is how you access the controls in a MasterPage

    Or maybe use a

    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sitemappath.aspx

    And place that inside some H1 tags. The SiteMapPath control feeds off of your SiteMap provider

  • Re: Adding a header on every content page

    10-06-2007, 12:55 PM
    Answer
    • Participant
      1,195 point Participant
    • eyad.salamin
    • Member since 05-19-2007, 5:59 PM
    • Posts 177

     

    jgd12345:
    1. Place <h1><%# SiteMap.CurrentNode.Title %></h1> where i want the header in every page but this means i have to call this.DataBind() in the page load of every page i wish to have a header and this is something i don't like doing.

    Why are you using the binding tags <%#  %>?? just use the rendering tags <%= SiteMap.CurrentNode.Title %>, this way you don't need to call DataBind();

    jgd12345:
    4. Place a literal within the h1 on every page and set it from within the master page.  I could not get this to work.  I tried doing ((Literal)this.Page.FindControl("litH1")).Text = SiteMap.CurrentNode.Title within the page load event handler of my master page but this didn't seem to do anything.

    Probably because the literal is in another naming  container. To solve this you can use this recursive search algorithm that i wrote to get you a reference to the specified control regardless whether the control was placed directly within the top level container or not.

    simply, in ur master page write the following code:

     

    protected void Page_Load(object sender, EventArgs e)
    {
    ((Label)FindMyChild(this.Page.Controls, "litH1")).Text = SiteMap.CurrentNode.Title;
    }

    public static Control FindMyChild(ControlCollection parent, string ID)
    {
    Control ret = null;
    foreach (Control c in parent)
    {
    ret = c.FindControl(ID);
    if (ret != null)
    break;
    else
    {
    ret = FindMyChild(c.Controls, ID);
    if (ret != null)
    break;
    }
    }
    return ret;
    }
      Finally, if it was me, I would have used a user control which renders the page title for the containing page and inserted that user control in every page.

    Hope this helps.
    <%= Eyad.Salamin %>

    <aspForums:DontForget>Don't forget to click on Mark as answer on the post that helped you.

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped. </aspForums:DontForget>
  • Re: Adding a header on every content page

    10-08-2007, 11:23 AM
    • Member
      487 point Member
    • jgd12345
    • Member since 06-22-2006, 5:28 PM
    • Posts 267

    I thought it was bad to use <%= ... %> in your asp.net code these days.  The recursive find control looks promising i will give that a go.  Cheers for your help.

Page 1 of 1 (5 items)