Use the RegisterCssFile method in a user control

Vote:
 
Just an idea that popped up: it might be useful to be able to use the "RegisterCssFile" method from a user control that inherits from UserControlBase, in the same way that you can register client script blocks. Currently the method only appears to be available in web forms that inherit from TemplatePage.
#18794
Mar 25, 2008 19:45
Vote:
 
I'm not sure what you're suggesting? You can use the RegisterCssFile from inside your UserControl by checking if the page "is a" TemplatePage, and cast it: // Inside an User Control inheriting // from UserControlBase ((TemplatePage)this.PageBase).RegisterCssFile("..."); Are you suggesting adding the RegisterCssFile method to the UserControlBase class?
#19028
Mar 25, 2008 19:46
Vote:
 
Thanks for the reply. This helps me achieve what I wasn't sure you could do from within a user control.
#19029
Mar 25, 2008 19:46
Vote:
 
Actually, I might have been a bit quick in saying 'thank you'. When trying the suggested code within a user control taht inherits from UserControlBase, I end up with the following server error: A control cannot modify its parents' control collections. I can add my css file when required by writing to an asp:literal, however using the RegisterCssFile method seemed more elegant and to the point.
#19030
Mar 25, 2008 19:46
Vote:
 
Thank you's are never wrong :-) The error you're getting is because the user control your code runs inside has been put into the section. This is the equivalent to lifting yourself by pulling your own pants. Well, almost. You're changing the control tree that you're code is running inside. Because this could lead to all kinds of nasty bugs and side effects, ASP.NET just won't allow it. Consider the scenario where you do this: Parent.Controls.Clear(); It would remove yourself from the control tree, which could be a problem for later rendering :-) So, you can still use RegisterCssFile method, just make sure the user control you call it from is not inside the head tag. If your code will always be somewhere inside the head tag, you really don't need the RegisterCssFile, as its prime purpose is to make sure the css file is only included once, and that the tag is placed inside the head tag, regardless from where you call it. In this case, a literal will be more effective. Just remember to put the runat="server" attribute on the head tag, if not, EPiServer won't find it, and no css file will be linked. In this case, the call returns false, and you could throw an exception. /Steve
#19031
Mar 25, 2008 19:46
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.