Google Analytics

Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Friday, February 11, 2011

Remove Default SharePoint Web Part CSS - Part 2

A post is long over due.  I wanted to revisit this topic for two reasons.  It was my first post ever, and according to best practices, this is not (necessarily) what you should do to remove SharePoint CSS classes from your custom developed web part.

If you follow best practices, you should not be inheriting from Microsoft.SharePoint.WebPartPages.WebPart.  According to SDK, "this class exists primarily for the purpose of backward compatibility, and secondarily, to provide a small set of features that are not available in the ASP.NET web part class."

The UseDefaultStyles Property, unfortunately, is available only in the Microsoft.SharePoint.WebPartPages.WebPart class and not in the System.Web.UI.WebControls.WebParts.WebPart class (the class you should really be using 99% of the time).

So...how do I fix my cumbersome SharePoint default CSS for my System.Web.UI.WebControls.WebParts.WebPart inherited class?!  This is what I came up with.

public class NoCssWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
      protected override void OnInit(EventArgs e)
      {
            base.OnInit(e);

            // No default styles
            if (this.Zone != null && this.Zone.WebPartChrome != null)
            {
                  SPChromeSettings chromeSettings = ((SPChrome)this.Zone.WebPartChrome).GetSPChromeSettings(this);
                  chromeSettings.UseDefaultStyles = false;
            }
      }

}

Tuesday, October 5, 2010

Remove Default SharePoint Web Part CSS

**UPDATE: See this revision.

It’s always been a struggle for me to style a web part in SharePoint because of the default Microsoft classes that get added (i.e. .ms-WPBody a:link, .ms-WPBody a:visited, blah, blah, blah).  These stylings are annoying and difficult to override because they’re directly on the web part and sometimes replace general site CSS.
Well…just turn them off.

For the DataViewWebPart...
<SharePoint:DataFormWebPart runat=”server” NoDefaultStyle=”true” …>
</SharePoint:DataFormWebPart>
(This also works for the CoreResultsWebPart as it’s essentially just a DVWP).

If you're creating your own web part...
public class NoCssWebPart : WebPart
{
      public NoCssWebPart()
     {
          this.UseDefaultStyles = false;
     }
}

References: