1. Add a new project (Class Library: jscontrol) to the solution)
2. Add the JS script (jscript1.js) to the new project)
FunctionTestalert (){
Alert("Hello! ");
}
3. Right-click jscript1.js and set build action to embedded resource.
4. Add a line to the assemblyinfo. CS file of the jscontrol project: (Note that jscontrol. jscript1.js is the namespace of the project where JS is located)CopySave
// Define metadata attributes of embedded resources in a component
[Assembly: system. Web. UI. webresource ("Jscontrol. jscript1.js","Application/X-JavaScript")]
5. Add a class for registering client scripts in the jscontrol project: referencescript. CSCopySave
NamespaceJscontrol
{
Public ClassReferencescript: system. Web. UI. webcontrols. webcontrol
{
Protected Override VoidOnprerender (eventargs E)
{
If(This. Page! =Null)
{
Clientscriptmanager manager =This. Page. clientscript;
Manager. registerclientscriptresource (Typeof(Referencescript ),"Jscontrol. jscript1.js");
}
Base. Onprerender (E );
}
}
}
6. Reference The jscontrol project in the website project and generate jscontrol. dll after compilation.
7. Register jscontrol. dll on the page to call the scriptCopySave
<% @RegisterAssembly ="Jscontrol" Namespace ="Jscontrol" Tagprefix ="Zhi" %>
8. reference scriptsCopySave
<Zhi: referencescript Id ="RS1"Runat ="Server"/>
<ASP: button Id ="Btnreadxml" Runat ="Server" TEXT ="Readxml" Onclick ="Btnreadxml_click"
Onclientclick ="Testalert ();" />
9.