Inline UserControl Macro Page.Header "Object reference not set to an instance of an object" error
I have an UserControl macro that when it is included in the HTML of a text area should inject the required javascriptand & css references required to make it render correctly.
This worked with regular asp.net sites that I have done before and kept the masterpage clean on pages where the usercontrol was not required.
In Umbraco I get a "Object reference not set to an instance of an object" in Page_Init for the Page.Header object. I am guessing that at the point when the inline usercontrol gets processed the Page.Header object does not exist.
Also, but I'm not 100% sure because I'm telling this from memory, I think I once solved this by using the Page.Header property of the current umbraco node. So I first got the current umbraco node by calling the method Node.GetCurrent(), and then I called Page.Header on the returned object.
thanks for the prompt reply. I have had a look at the link and it seems ok but splitting my usercontrol into two items in order to inject the header links seems messy. I am trying to make this as easy as possible so that my client can inject the control into a HTML text area and it just works.
Your solution sounds interesting and I have got the current node as a variable but cannot see a way to reference the Page.Header. Can you remember any more detail of how you solved it?
I checked my code, and in fact I use this from a masterpage and not from a user control, this is probably why I get "easy" access to the Page.Header. Another difference though, is that I access it from the OnLoad event, and not the OnInit event. This might do the trick too...
Sorry I did not come out with an effective solution...
Found a workaround. The user control puts the css & js links in the HttpContext.Current.Items Dictionary on Init. Then a PageAdapter inject the links before the end Head tag:
User Control:
Partial Class usercontrols_website_EventsCalendar Inherits System.Web.UI.UserControl
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
RegisterScripts() RegisterCSS()
End Sub
Private Sub RegisterScripts()
If Not HttpContext.Current.Items.Contains("js_FullCalendar") Then HttpContext.Current.Items.Add("js_FullCalendar", "<script type=""text/javascript"" src=""" & Page.ResolveUrl("~/scripts/fullcalendar/fullcalendar.js") & """></script>") End If
If Not HttpContext.Current.Items.Contains("js_FullCalendarMin") Then HttpContext.Current.Items.Add("js_FullCalendarMin", "<script type=""text/javascript"" src=""" & Page.ResolveUrl("~/scripts/fullcalendar/fullcalendar.min.js") & """></script>") End If
End Sub
Private Sub RegisterCSS()
If Not HttpContext.Current.Items.Contains("css_FullCalendar") Then HttpContext.Current.Items.Add("css_FullCalendar", "<link href=""" & Page.ResolveUrl("~/css/fullCalendar.css") & """ rel=""stylesheet"" type=""text/css"" />") End If
Public Class ResourcePageAdapter Inherits System.Web.UI.Adapters.PageAdapter
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
MyBase.Render(htmlWriter)
Dim html As String = stringWriter.ToString()
Dim StartPoint As Integer = html.IndexOf("</head>")
For Each de As DictionaryEntry In HttpContext.Current.Items If de.Key.StartsWith("js_") Or de.Key.StartsWith("css_") Then html = html.Insert(StartPoint, de.Value & vbCrLf) End If Next
Inline UserControl Macro Page.Header "Object reference not set to an instance of an object" error
I have an UserControl macro that when it is included in the HTML of a text area should inject the required javascriptand & css references required to make it render correctly.
This worked with regular asp.net sites that I have done before and kept the masterpage clean on pages where the usercontrol was not required.
In Umbraco I get a "Object reference not set to an instance of an object" in Page_Init for the Page.Header object. I am guessing that at the point when the inline usercontrol gets processed the Page.Header object does not exist.
Is there a way of doing this?
Hi Simon,
You might have a look at the following post, I think it deals with the same kind of problem: http://our.umbraco.org/forum/developers/api-questions/5111-Accessing-Page-object-from-UserControl
Also, but I'm not 100% sure because I'm telling this from memory, I think I once solved this by using the Page.Header property of the current umbraco node. So I first got the current umbraco node by calling the method Node.GetCurrent(), and then I called Page.Header on the returned object.
Hope it helps!
Cheers,
Michael.
Hi Michael,
thanks for the prompt reply. I have had a look at the link and it seems ok but splitting my usercontrol into two items in order to inject the header links seems messy. I am trying to make this as easy as possible so that my client can inject the control into a HTML text area and it just works.
Your solution sounds interesting and I have got the current node as a variable but cannot see a way to reference the Page.Header. Can you remember any more detail of how you solved it?
Hi Simon,
Unfortunately I don't remember much more, so I'll have to check tonight when I am back at the office. I keep you posted!
Cheers,
Michael.
Hi Simon,
I checked my code, and in fact I use this from a masterpage and not from a user control, this is probably why I get "easy" access to the Page.Header. Another difference though, is that I access it from the OnLoad event, and not the OnInit event. This might do the trick too...
Sorry I did not come out with an effective solution...
Cheers,
Michael.
Found a workaround. The user control puts the css & js links in the HttpContext.Current.Items Dictionary on Init. Then a PageAdapter inject the links before the end Head tag:
User Control:
PageAdapter:
is working on a reply...