ASP.NET 将JS文件封装成DLL

第一步:创建一个项目(工程)

命名随便自己 之后找到AssemblyInfo.CS文件,在这个文件里添加资源文件。添加到文档最尾部如下代码

[assembly:WebResource( " ClassLibrary1.JScript1.js " , " application/x-javascript " )]

 

里面的ClassLibrary1是工程的命名空间,如果没有命名空间可以不用写上,只要把文件名写上就可以了

创建一个类文件 这里由于我是写的DEMO就没有太规范,就用的Class1

 

代码
public class Class1 : WebControl
     {
        
protected override void OnPreRender(EventArgs e)
         {
            
if ( this .Page != null )
             {
                
if (manager_JSname == " JScript1 " )
                 {
                     ClientScriptManager manager1
= this .Page.ClientScript;
                     manager1.RegisterClientScriptResource(
typeof (Class1), " ClassLibrary1.JScript1.js " );
                 }
                
else if (manager_JSname == " JScript2 " )
                 {
                     ClientScriptManager manager2
= this .Page.ClientScript;
                     manager2.RegisterClientScriptResource(
typeof (Class1), " ClassLibrary1.JScript2.js " );
                 }

             }
            
base .OnPreRender(e);

         }
        
private string manager_JSname;

        
public string Manager_JSname
         {
            
get { return manager_type; }
            
set { manager_type = value; }
         }
     }

 

 

之后在页面文件上写

页面头部写上

<% @ Register Assembly = " ClassLibrary1 " Namespace = " ClassLibrary1 " TagPrefix = " cc1 " %>

内容中间

   < cc1:Class1 runat = " server " ID = " class1 " manager_JSname = " JScript1 " ></ cc1:Class1 >

你可能感兴趣的:(JavaScript,.net,asp.net,asp)