This is a migrated thread and some comments may be shown as answers.

Adding a Stylesheet from inside a UserControl

4 Answers 242 Views
ScriptManager and StyleSheetManager
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 05 Nov 2010, 08:23 AM
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) +

4 Answers, 1 is accepted

Sort by
0
Stuart Hemming
Top achievements
Rank 2
answered on 05 Nov 2010, 08:40 AM
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
0
Stuart Hemming
Top achievements
Rank 2
answered on 05 Nov 2010, 09:09 AM
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

0
Yana
Telerik team
answered on 10 Nov 2010, 04:53 PM
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
0
Stuart Hemming
Top achievements
Rank 2
answered on 10 Nov 2010, 05:05 PM
Yana,

Thanks for the pointer.

-- 
Stuart
Tags
ScriptManager and StyleSheetManager
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Stuart Hemming
Top achievements
Rank 2
Yana
Telerik team
Share this question
or