4

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

  • Make sure an Assembly directive appears at the top of the ASCX page, that points to the Microsoft.SharePoint assembly, and that the SharePoint TagPrefix points to this assembly. – James Love Oct 9 '11 at 17:26
  • Okay. If i add <SharePoint:CssRegistration runat="server" Name="./Styles/Site.css" /> in the .ascx file, some code will automatically added to the .ascx.gcs file. Must i add the Microsoft.SharePoint assembly to the .ascx file? At the moment, the assembly is in the .ascx.gcs file. – user5167 Oct 9 '11 at 17:50
  • Can't quite remember how the plugin generates the ascx.g.cs file, but try manually adding the Assembly directive to the .ascx file. – James Love Oct 9 '11 at 17:55
3

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.

  • In the ascx.g.cs file the some CssRegistration code will add automatically. And here Visual Studio tells me, that a assembly is missing. This is the code:private global::Microsoft.SharePoint.WebControls.CssRegistration @__BuildControl__control2() { global::Microsoft.SharePoint.WebControls.CssRegistration @__ctrl; @__ctrl = new global::Microsoft.SharePoint.WebControls.CssRegistration(); @__ctrl.Name = "./Styles/Site.css"; return @__ctrl; } – user5167 Oct 9 '11 at 17:55
  • Like I said in the comment thread above, try adding the SharePoint Assembly to the Assembly ASP.NET Directive at the top of the ascx file. – James Love Oct 9 '11 at 17:57
  • Okay, atm i haven't access to the development pc. Do u mean something like <%@ Assembly Name="Microsoft.SharePoint" %> in the ascx. – user5167 Oct 9 '11 at 17:59
  • Exactly that, yeah. Though it might need a fully qualified assembly name (including the Version, Culture and Public Key Token). – James Love Oct 9 '11 at 18:24
  • Okay, i will test it tomorrow. But why i get the assembly warning message in the ascx.g.cs file, that's strange... In the the ascx.g.cs file i use the Microsoft.SharePoint assembly. Will will let u know if it's work. So far many thanks! – user5167 Oct 9 '11 at 18:48
1

If you also will need to add JS code, consider using the ScriptManager. See my answer on that topic here

  • 1
    Merci. That would be the next step. The asp and c# code is ready, just the css and js are missing. I will give you tomorrow feedback if every think works fine. Thanks! – user5167 Oct 9 '11 at 19:28
  • of course this is not a Sandboxed solution, isn't it? – superjos Nov 4 '11 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy