. NET encapsulates JS files into DLL and calls them on the page

Source: Internet
Author: User

. 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>

Related Article

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.