Saturday, March 24, 2012

Deploying Styles with your controls

I have found a way of deploying styles for an ajax control which I thought might be useful to others. I found this while examining the source code of the AjaxControlToolkit which is available as an open source project. you will need a reference to this assembly in order to use this method. These are the steps to be taken.

1.Include your css file in your controls project, set it as an embeded resource and and an assembly attribute for your control refrencing said css file. If you don't know how to do this google for it, a million people have posted on this subject.

[assembly:WebResource("MyControls.Web.Controls.DropDown.DropDown.css","text/css", PerformSubstitution =true)]

2. add the ClientCssResource to your class

[ClientCssResource("MyControls.Web.Controls.DropDown.DropDown.css")]

3. In your Prerender method for your control call the RegisterCssReference method of the static class ScriptObjectBuilder which is part of the AjaxControlToolkit

ScriptObjectBuilder.RegisterCssReferences(this);

This ScriptObjectBuilder class is an interesting class, it looks like it keeps a cached set of refrences to the css files included so as not to included them more than once. It adds the list of css refrences it has compiles as link tags to the <HEAD> of your page. Make sure your <HEAD> has runat="server" set. If you need a more complete example of how this is used , you might read the cs source code for the DropDownExtender in the toolkit.

Thanks fbaldoni!

I have been searching a half day to get this to work. This only part I had missing was step 3. Where in the AJAX documentation can I find that? No where. I just happen to stumble upon your post.


Glad to be of serviceSmile

No comments:

Post a Comment