Adding reference to a CSS in a webpart programmatically


Think if you were selling a custom webpart you would like all the css referred in your webpart correctly and use a layouts folder for deploying your custom CSS. This is a very common requirement and any easy one ! But its interesting to note that you can use the CssRegistration Control to refer a css without using a visual webpart ! Have the OnLoad overriden and add your custom css there!

 

protected override void OnLoad(EventArgs e)

{

    base.OnLoad(e);

    CssRegistration.Register(SPUtility.GetServerRelativeUrlFromPrefixedUrl("~sitecollection/_layouts/custom/webpart.css"));

}

Advertisements

One Comment on “Adding reference to a CSS in a webpart programmatically”

  1. robertosljunior July 29, 2011 at 3:23 pm #

    Very cool

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: