Several methods of embedding JS scripts into components

Source: Internet
Author: User

Several Methods to embed JS scripts into components:

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/JavaScript")]

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

Clenbuterol Hydrochloride www.chouclub.com

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.