Understanding problem: How to add a stylesheet to a web part
-
06-12-2019 - |
문제
Well, I try to add a css stylesheet to my web part but it didn’t work… I read that there are many ways to a css file into a web part, but I didn’t understand them….
First I installed the Sharepoint 2010 Tools, to create a visual web part for sandboxing. After that I created an empty project and added a visual web part (sandbox) to the project. Just after that, I created a new folder in the visual web part and added the stylesheet file to them.
Now I will add this css file to my web part, but I don’t know how…
I read, that this I possible with:
<SharePoint:CssRegistration runat="server" Name="./Styles/Site.css" />
But Visual Studio tells me, that an assembly for ::Microsoft.SharePoint.WebControls.CssRegistration
is missing.
I use also use using Microsoft.SharePoint
;
What is the best way, to import a css file to a web part? The css file should just available for this web part.
It would be awesome, if someone could help me to understand, how to include a stylesheet.
Information: CssRegistration: Available in Sandboxed Solutions: No!
Greetz
해결책
Another method to add the CSS is to create the CssRegistration object in the C# codebehind for the web part, and add your instance of CssRegistration to the web part's control hierarchy using this.ChildControls.Add()
.
You'll need to ensure this code is done within CreateChildControls override of the web part.
다른 팁
If you also will need to add JS code, consider using the ScriptManager. See my answer on that topic here