Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Osborne 108 posts 150 karma points
    Nov 30, 2010 @ 09:32
    Simon Osborne
    0

    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?

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 30, 2010 @ 09:53
    Michael Latouche
    0

    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.

  • Simon Osborne 108 posts 150 karma points
    Nov 30, 2010 @ 10:33
    Simon Osborne
    0

    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?

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 30, 2010 @ 10:55
    Michael Latouche
    0

    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.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Dec 01, 2010 @ 14:17
    Michael Latouche
    0

    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.

  • Simon Osborne 108 posts 150 karma points
    Dec 02, 2010 @ 13:20
    Simon Osborne
    0

    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

    End Sub

    End Class

    PageAdapter:

    Imports Microsoft.VisualBasic
    Imports System.Collections.Generic


    Namespace Control2K.Web.Adapters

    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

    End Sub

    End Class

    End Namespace

     

Please Sign in or register to post replies

Write your reply to:

Draft