第一步:創建一個項目(工程)
命名隨便自己 之后找到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; }
}
}
{
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>