Usually in ASP. NET, we often use JS scripts to implement many operations on the page. There are also several methods to reference JS on the page.
Here we will introduce how to embed JS scripts into the Assembly (DLL. The biggest advantage of this approach is that it can protect the content of JavaScript files and prevent the definition of JavaScript classes from being modified.
The following is an example to illustrate how to operate:
1. Open VS and create a WebApplication1 project [here you can also create a website]. Use the default settings.
2. Add a new project in the current solution. (Class Library: JSLib)
After the file is added, a Class. cs file is created by default. We will rename it to [ReferenceClientScript. cs], which will be used later.
3. Because the new class library is created, you need to add reference: System. Web, which will be used later.
4. Add a JS file to JSLib and use the default settings. There is a JScript1.js file automatically.
5. Compile the JS script [JScript. js ].
Here is just a simple demonstration, so the script code should be as simple as possible. Hello, World !, The simplest class is intended for demonstration. Of course you can write more complex.
JSScript. js code:
Function Hello (){
Alert ("Hello, world! ");
}
6. Set JScript. js attributes-> Generate an operation to [embedded resource]
7. Add a line of code at the end of the AssemblyInfo. cs file of the JSLib project: [Note: JSLib. JScript1.js. JSLib is the namespace of the js project]
[Assembly: WebResource ("JSLib. JScript1.js", "text/javascript")]
8. Compile the JS script registration class [ReferenceClientScript. cs ].
ReferenceClientScript. cs code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace JSLib
{
Public class ReferenceClientScript: System. Web. UI. WebControls. WebControl
{
Protected override void OnPreRender (EventArgs e)
{
If (this. Page! = Null)
{
ClientScriptManager manager = this. Page. ClientScript;
Manager. RegisterClientScriptResource (typeof (ReferenceClientScript), "JSLib. JScript1.js ");
}
Base. OnPreRender (e );
}
}
}
9. Add a project reference in WebApplication1. JSLib. dll generated after compilation
10. Register JSLib. dll on the page to call the script
<% @ Register Assembly = "JSLib" Namespace = "JSLib" TagPrefix = "JSLibInstance" %>
11. reference the script.
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "WebApplication1. _ Default" %>
<% @ Register Assembly = "JSLib" Namespace = "JSLib" TagPrefix = "JSLibInstance" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
<Div>
<JSLibInstance: ReferenceClientScript id = "js1" runat = "server"> </JSLibInstance: ReferenceClientScript>
JS is encapsulated into DLL and Demo is called on the page. <Br/>
<Input id = "Button1" type = "button" value = "button" onclick = "Hello ();"/>
</Div>
</Form>
</Body>
</Html>
12. Completed
13. Check the source code and you will find that all js references are encapsulated into. axd. For example, <script src = "/WebResource. axd? D = ojev8gysmjasgicbjv7ux0vxuplb1juude0qlblln9w1 & t = 634125776275000000 "type =" text/javascript "> </script>
<Script src = "/ScriptResource. axd? D = plain & t = 7528725e "type =" text/javascript "> </script>