简体   繁体   中英

Custom CSS Property in Custom Sharepoint 2010 WebPart

I'm working on my first web part and was wondering if there is a way to create a custom resource (like a WPProperty) that would link CSS to the webpart just as you can control the XSLT of a XSLTViewPart (which, ironically I'm inheriting from). The reason for this is we're looking to make a shareable resource that would work with different lists that we want to represent differently.

I was thinking of adding a CSSLink or CSSRegistration into the code-behind, but then I thought it would be packaged along with the .DLL and not editable. Am I wrong?

To add a CSS file to a Web Part you can do following.

  1. Map Layouts folder in Visual Studio

  2. Add CSS file to the Mapped Folder (ie: If the Project Name is SPTest then add the file to Layouts/SPTest/ folder)

Then add following code to the WebPart's .ascx file

<Sharepoint:CssLink ID="CssLink1" runat="server" />
<SharePoint:CssRegistration ID="CssRegistration1" Name="/_layouts/SPTest/Test.css" runat="server" />

SharePoint 2010 doesn't really need the Sharepoint:CssLink.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM