Adding a Stylesheet from inside a UserControl

5 posts, 0 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 05 Nov 2010 Link to this post

    I'm loading a UserControl from my page markup.

    The page has a RadStylesheetManager on it.

    I want to ensure my page has a particular stylesheet loaded and I don't want to rely on the developer using the control loading the stylesheet on the page explicitlt. So I thought that something like this might work ...
    RadStyleSheetManager mgr = Page.Items[typeof(RadStyleSheetManager)] as RadStyleSheetManager;
    if (mgr != null)
    {
        StyleSheetReference ss = new StyleSheetReference();
        ss.Path = Page.ResolveUrl("~/StyleSheets/Popup/InlinePopups.css");
        ss.Name = "InlinePopups";
        mgr.StyleSheets.Add(ss);
    }

    Now mgr is being correctly populated. But when the page runs, I get this ...
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
     
    Source Error:
     
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
     
    Stack Trace:
     
     
    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Web.UI.ScriptEntrySlot.GetSerializedAssemblyInfo(ScriptEntry scriptEntry) +208
       Telerik.Web.UI.ScriptEntrySlot.SerializeScriptEntry(ScriptEntry scriptEntry) +71
       Telerik.Web.UI.ScriptEntryUrlBuilder.TryAddScriptEntry(ScriptEntry scriptEntry) +56
       Telerik.Web.UI.ScriptEntryUrlBuilder.RegisterScriptEntry(ScriptEntry scriptEntry) +237
       Telerik.Web.UI.RadStyleSheetManager.Page_PreRenderComplete(Object sender, EventArgs e) +373
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8701886
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1029

    So, what am I doing wrong?

    -- 
    Stuart
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace: 


    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Web.UI.ScriptEntrySlot.GetSerializedAssemblyInfo(ScriptEntry scriptEntry) +208
       Telerik.Web.UI.ScriptEntrySlot.SerializeScriptEntry(ScriptEntry scriptEntry) +71
       Telerik.Web.UI.ScriptEntryUrlBuilder.TryAddScriptEntry(ScriptEntry scriptEntry) +56
       Telerik.Web.UI.ScriptEntryUrlBuilder.RegisterScriptEntry(ScriptEntry scriptEntry) +237
       Telerik.Web.UI.RadStyleSheetManager.Page_PreRenderComplete(Object sender, EventArgs e) +373
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8701886
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +10
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace: 


    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Web.UI.ScriptEntrySlot.GetSerializedAssemblyInfo(ScriptEntry scriptEntry) +208
       Telerik.Web.UI.ScriptEntrySlot.SerializeScriptEntry(ScriptEntry scriptEntry) +71
       Telerik.Web.UI.ScriptEntryUrlBuilder.TryAddScriptEntry(ScriptEntry scriptEntry) +56
       Telerik.Web.UI.ScriptEntryUrlBuilder.RegisterScriptEntry(ScriptEntry scriptEntry) +237
       Telerik.Web.UI.RadStyleSheetManager.Page_PreRenderComplete(Object sender, EventArgs e) +373
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8701886
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +
  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 05 Nov 2010 Link to this post

    I've got a workaround by doing this instead...
    Page.Header.Controls.Add(new Literal
    {
        Text = String.Format("<link href='{0}' rel='stylesheet' type='text/css' />", Page.ResolveUrl("~/StyleSheets/Popup/InlinePopups.css"))
    });

    But then the RadStyleSheetManager ignores is. That is the stylesheet is loaded separately. So it's not an ideal solution.

    -- 
    Stuart
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 05 Nov 2010 Link to this post

    Hmm.

    A little digging seems to suggest that the boys and girls at telerik towers believe I can be trusted not to start world war three by including code like this ...
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Name="http://www.WorldDominationRUs.com/Countdown.js" />
        </Scripts>
    </telerik:RadScriptManager>

    ... but not to format it badly by saying ...
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server">
        <StyleSheets>
            <telerik:StyleSheetReference Path="http://www.WorldDominationRUs.com/Nasty.css" />
        </StyleSheets>
    </telerik:RadStyleSheetManager>

    Go figure!

    -- 
    Stuart

  5. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 10 Nov 2010 Link to this post

    Hello Stuart,

    It's expected that adding css styles with Page.Header will be ignored by RadStyleSheetManager.

    Please check this help article - it explains in details how to add custom styles to the stylesheetmanager, also a sample project is attached. Note that the css files should be marked as embedded resource and should be placed inside a webresource assembly.

    Hope this helps.

    Greetings,
    Yana
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 10 Nov 2010 Link to this post

    Yana,

    Thanks for the pointer.

    -- 
    Stuart
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017