Sample Download: http://code.msdn.microsoft.com/CSASPNETLoadAndExecuteDynam-25a1f05a
The sample is provided by Microsoft engineer Sagar Bhanudas Joshi.
You can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.
Building the Sample
- Added a SVC (Ajax enabled web service [WCF] to the project).
- Added a ScriptManager Object to the Default.aspx page for asynchronously calling the method in the WCF service.
- We call the method in WCF service Asynchronously using the "$.Ajax()" method. in JQuery
- The result of the service call is the path to the script object on the server.
- Server contains a folder named a "Scripts" which is only known to the WCF service.
- Then dynamically, the demanded script is linked to the page.
- For sample, we just have a "alert()" in the dynamic script that is loaded and executed.
Running the Sample
- Open the “CSASPNETLoadAndExecuteDynamicJS.sln” file in Visual Studio 2010.
- Run the sample.
- Click the “Load Script stored on Internal Server” button.
- This will attach the script on-demand to the page.
- Click the “Call Script stored on Internal Server” button.
- This will call the method from the dynamically loaded /linked scripts.
- For external scripts enter the script URL in the text box provided.
- Valid link will attach the external script to the page.
The following image is the screenshot.
Using the Code
2) We use the “$.ajax()” method from JQuery to asynchronously call the web method.
3) The parameter to the service is the name of the script that is to be linked on demand.