. Net encapsulates js files into dll and calls them on the page
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:
The following is a code snippet:
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/Webpage effects")]
8. Compile the js script registration class [referenceclientscript. cs ].
Referenceclientscript. cs code:
The following is a code snippet:
Using system;
Using system. collections. generic;
Using system. linq;
Using system. text;
Namespace jslib
{Www.yzjjx.com
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
The following is a code snippet:
<% @ Register assembly = "jslib" namespace = "jslib" tagprefix = "jslibinstance" %>
11. reference the script.
The following is a code snippet:
<% @ Page language = "c #" autoeventwireup = "true" codebehind = "default. asp tutorial x. cs" inherits = "webapplication1. _ default" %> yzyedu.com
<% @ 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>