List several ways to embed script files in a set of programs:
1. this. Page. ClientScript. RegisterClientScriptBlock: register the script.
2. this. Page. ClientScript. RegisterClientScriptInclude: Reference script (URL address)
3. this. Page. ClientScript. RegisterClientScriptResource: Reference name (for example, MyAssembly. Js. MyJs. js)
Here I will talk about the usage of this. Page. ClientScript. RegisterClientScriptResource, and embed the script file in the program set.
Step 1: Create an assembly: MyAssembly
Step 2: Create a JS file: Save the directory Js/MyJs. js under the current Assembly
Step 3. Right-click "MyJs. js" and set "generate operation" to "embedded resource ".
Step 4: create a user control: MyControl, used to register Js
Step 5: rewrite the OnPreRender event of MyControl (not required. You can change to another loading event)
/// <Summary>
/// Register the verification script file
/// </Summary>
/// <Param name = "e"> </param>
Protected override void OnPreRender (EventArgs e)
{
This. Page. ClientScript. RegisterClientScriptResource (typeof (MyControl ),
"MyAssembly. Js. MyJs. js ");
Base. OnPreRender (e );
}
Step 6: Add a label under AssemblyInfo. cs of the current Assembly
[Assembly: System. Web. UI. WebResource ("MyAssembly. Js. MyJs. js", "application/x-javascript")]
So far, when MyControl is used in the page, the js file can be automatically output.
Next I will introduce you to something important. When debugging a page with MyControl, we will see the following content:
On my side, open WebResource_2.axd and you will see the script content I wrote in the Assembly,
That is to say, all the script files embedded in the Assembly are obtained by requesting WebResource_2.axd.
Is encrypted to indicate the JS file of the current request.
Play birds!