I checked the information on the Internet today and accidentally found the dll Control to add external CSS. The last time I made the control, I finally solved some problems.
AlthoughArticleI am not good at English, but the example is simple and clear.
Listing 1: assemblyinfo. CS entries
[Assembly: system. Web. UI. webresource ("Myimage.gif ","Img/GIF")] [Assembly: system. Web. UI. webresource ("Mystylesheet.css","Text/CSS")] [Assembly: system. Web. UI. webresource ("Myjavascript. js","Text/JS")]
Namespace note
The project's default namespace (defined in the Application tab of the project's properties page) will be added as a prefix to the filename of embedded resources. in this case, I 've set the default namespace to an empty string. otherwise, the tag's first parameter wocould need to be defaultnamespace. filename. extension instead of simply filename. extension. (This was the biggest pitfall I encountered because it wasn' t obvious that the namespace wocould be added as a prefix, and so I was referencing the resources by their short names when I shoshould have been using the long format .)
Accessing the embedded Resource
To add the embedded resource to our ASP. NET page, we will be calling the clientscriptmanager's getwebresourceurl method. its first parameter is the type of the control's class (which will eventually provide. net with an Assembly reference where the embedded resource is contained) and its second parameter is the name of the resource as specified in the assemblyinfo. CS file. for example, to load an image in an image control, use the code in Listing 2. to add a stylesheet to the page header area, use the code in listing 3. to render a javascript include tag, use the code in Listing 4.
Listing 2: setting an image control's source
Image theimage=NewImage (); theimage. imageurl=Page. clientscript. getwebresourceurl (This. GetType (),"Myimage.gif");
Listing 3: adding a stylesheet to the page header
String includetemplate = "<LINK rel = 'stylesheet 'text = 'text/CSS' href = '{0}'/>" ; String Includelocation = Page. clientscript. getwebresourceurl (This . GetType (), "Mystylesheet _links.css" ); Literalcontrol include = New Literalcontrol (string. Format (includetemplate, includelocation); (htmlcontrols. htmlhead) page. header). Controls. Add (include );
Listing 4: rendering a javascript include
string scriptlocation = page. clientscript. getwebresourceurl ( This . getType (), " msdwuc_windowstatus.js "); page. clientscript. registerclientscriptinclude (" msdwuc_windowstatus.js ", scriptlocation);
Clientscriptmanager quirks
One of the things I encountered when working with this technique was that most examples of server controls do their "thing" during the render event. so, I attempted to be a good way ate citizen and have my controls behave the same way and I found that I cocould not use registerclientscriptblock at this point in the page life cycle. registerstartupscript seemed to work fine in this scenario and IE 6.0 apparently tolerates rendering styles at this point and applying them to the page's rendered contents, but I wasn't happy with that. so, I discovered that I had to make all callto registerclientscriptblock on or before the onprerender event for them to be added to the correct part of the page.
Conclusion
We 've covered how you can embed resources in your server control projects and how to reference them in a Web environment. this shoshould simplify your life from a deployment perspective, and shoshould make your server controls tighter now that they can leverage static images and files, and do not require any installation other than deployment of the compiled assembly.
I hope it will help others.