but if you have, read along. i know i have as i think i've always taken for granted the feature of how this is being accomplished in both the ajax tools as well as some things that asp.net 2.0 uses as well...so let's see what's going on here.
if you have no idea what i'm talking about, basically load up an asp.net ajax page and look at the source, you'll see something like this:
first, why would you want to do this? well, good question -- i don't think you'd want to do this for everything, but i think it is a good idea for reusable components...and i think we'll see this trend more and more from component vendors -- it reduces the installation woes for some of the client-side script deployment.
anyhow, i'm babbling...here's the goods.
after that go into your class file that would render your control/component or wherever you'll want the embedded script to "reside." the easiest thing here is to add this to your code:
3: namespace ScriptResourceClass
5: public class Class1
7: public string SayHelloServer()
9: return "Hello World from server";
once that is there (you may have to add a reference to System.Web.dll) then you are ready to go. now consuming (or should i say exposing) this is VERY simple using asp.net ajax and the wonderful do-all-the-heavy-lifting-management-for-me scriptmanager component. in our page where we want to consume this and add a script reference to the scriptmanager like this:
1: <asp:ScriptManager ID="ScriptManager1" runat="server">
3: <asp:ScriptReference Assembly="ScriptResourceClass"
4: Name="ScriptResourceClass.HelloWorld.js" IgnoreScriptPath="true" />
when rendered you'll see some line that looks like this:
i'm not going to pretend that i know what/how the webresource.axd url is generated and formatted, but i trust it is something super cool and important. note you aren't limited to using scripts for embedded resource...you can just as easily put a dependent image into this format and have your control reference it in the image path using WebResource("name") and then it will get it from the embedded resource. once this webresource.axd url is there, now my SayHello() function from *script* works fine when called from anywhere.
so what if you don't use asp.net ajax? well, shame on you then. but seriously, you can still accomplish this, but you just have to write a bit more code in your control rendering to retrieve the resource and ensure it is registered on the page. you can use something like the Page.ClientScript.GetWebResourceUrl function.