Links

 

Adding a CSS stylesheet to a UserControl

User Controls can have their own CSS stylesheets, which can get added to the Page's <HEAD> element when the control is loaded. In order to do this, the following needs to be done. 1. Add the RUNAT attribute to the page's HEAD element.
<head runat="server">
2. Make sure that the HEAD element does not contain any <% %> or <%= %> tags. This method will not work, if the head contains these tags. 3. Create a CSS stylesheet file. 4. In the Page_Load event, add the following code.
Page.Head.Controls.Add("<link href=\"/FileName.css\" rel=\"stylesheet\" type=\"text/css\" />");
When the user control is now loaded, the stylesheet (in this case FileName.css) will be added into the Head element of the Page.
Submit a comment, suggestion, or additional information about this snippet
If you login or register, you'll be able to post a comment or provide feedback about this snippet.
Contact Us | Terms of Use