July 2, 2008 by Christoff Truter ASP.NET
I've written a number of web controls for ASP.net over the
years, and one of the issues I had working with ASP.net 1.0 was how to sufficiently
handle static files that ships with my controls (javascript, CSS, images etc).
When ASP.net 2.0 came along, it introduced the capability of embedding these static
files into your controls' assembly, and allow you to reference them inside your
control, inturn serving resources as axd files to the browser. (functionality extensively
being ab... uhm used by Ajax.net)
In my demo we're going to write a very simple control that collapses content on
a page, we've got two images and a piece of javascript that we're going to embed.
First off you will need to make sure that your static files are set as embedded
resources in its build action property, like demonstrated in the image below.
[assembly: WebResource("DemoControl.images.down.jpg", "image/jpg")] [assembly: WebResource("DemoControl.images.up.jpg", "image/jpg")] [assembly: WebResource("DemoControl.javascripts.collapse.js", "text/javascript", PerformSubstitution = true)]
function toggle(sender, e) { var content = document.getElementById(e); switch(content.style.display) { case "none": content.style.display = "block"; sender.src = '<%= WebResource("DemoControl.images.up.jpg")%>'; break; case "block": content.style.display = "none"; sender.src = '<%= WebResource("DemoControl.images.down.jpg")%>'; break; } }
private string WebResource(string resourceName) { return Page.ClientScript.GetWebResourceUrl(this.GetType(), resourceName); }
December 3, 2008 by Christoff Truter
I figured you didn't see it, since you pointed out using System.Web.UI