views:

246

answers:

2

In the Page load of A control the Page.Header is null when I am attempting to add a reference. Is there anything special I have to do to add a reference to the head of a page from a control.

Maybe a better way to as this is when does Page.Header load or when can it be accessed from a control

+1  A: 

You can just add an event handler to Page Loaded Event inside Load Event of control and do what you want.

Something like that:

    this.Page.LoadComplete += (ObjectSender, ev) =>
    {
        var mStyle = new Style();
        mStyle.BorderWidth = new Unit(5);
        Page.Header.StyleSheet.CreateStyleRule(mStyle, null, "body");
    };

ps. I used expression lambda for simplicity.

Cleiton
Page.Header is still null
Are you using Master Pages? If you are, use master page event instead.
Cleiton
Sometimes the header control is inside a masterpage other times it is on the page itself, all times page.Header seems to be null
+2  A: 

Be sure to set your head tag runat="server"

<head runat="server">
..
</head>

otherwise, the reference to Page.Header will always be null.

Tom