ASP.NET 2.0为我们提供了ClientScriptManager.RegisterClientScriptInclude 方法向页面加载JS文件.虽然这样在页面运行时动态加载JS,但是我们必须事先编写好JS文件以便页面加载.
所以在JS文件数量很大时这个方法并不是很"实在".
在这里我介绍一种我最近发现的动态加载的方法一般大家参考
首先,我们在项目中添加一个JS.ASPX的文件.
接下来我们就要重写页面的Render方法)
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class JS : System.Web.UI.Page
13
2
3
4
5
6
7
8
9
10
11
12
13
这里的工作我们就完成了.
接下来我们创建一个JS文件来配合这段代码使用
很简单吧.
现在就来添加一个调用这两个JS文件的页面,同样也很简单
1
<%@ Page Language="C#" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<script runat="server">
6
7
string Param = "2";
8
</script>
9
10
<html xmlns="http://www.w3.org/1999/xhtml" >
11
<head id="Head1" runat="server">
12
<title>无标题页</title>
13
<script type="text/jscript" language="jscript" src="JScript.js"></script>
14
<script type="text/javascript" language="javascript" src="JS.aspx?param=<%=Param %>"></script>
15
</head>
16
<body>
17
<form id="form1" runat="server">
18
<input id="Button1" type="button" value="button" onclick="Test()" />
19
</form>
20
</body>
21
</html>
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
电脑上没有抓图软件,所以就不给大家看结果了
(小弟第一次写东西,大家多包含)