Embed JS script files in the Assembly

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.