1.使用Page.ClientScript.RegisterClientScriptBlock
使用 Page.ClientScript.RegisterClientScriptBlock可以防止javascript 函数放置在page的顶部
对于该输出,请注意:通过使用 RegisterClientScriptBlock,JavaScript 函数紧跟在 HTML 代码中开启元素 <form> 的后面。
下面就是个例子
前置文件4-10.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
4-10.aspx.cs
"
Inherits
=
"
_4_10
"
%>
<!
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
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:Button
ID
="button1"
runat
="server"
Text
="click"
OnClientClick
="AlertHello()"
/>
</
div
>
</
form
>
</
body
>
</
html
>
后置文件4-10.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
_4_10 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
String myScript
=
@"
function AlertHello(){alert('hello World');}
"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"
MyScript
"
, myScript,
true
);
}
}
运行一下看到什么??点击 "click"按钮得到了一个alert。不过你仔细检查我们代码看到什么?发现我们没有写<script type="text/javascript"></script> 这样的开始终止呢。原来Page.ClientScript.RegisterClientScriptBlock已经自动的帮我们生成了。当然这个是在你的function还算是比较大的情况下使用这。
上面一个是正确的例子,来分析一个异常的例子
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
4-10.aspx.cs
"
Inherits
=
"
_4_10
"
%>
<!
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
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:TextBox
ID
="TextBox1"
runat
="server"
>
Hello ASP.NET
</
asp:TextBox
>
</
div
>
</
form
>
</
body
>
</
html
>
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
_4_10 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
String myScript
=
@"
alert(document.forms[0]['TextBox1'].value);
"
;
Page.ClientScript.RegisterClientScriptBlock(
this
.GetType(),
"
MyScript
"
, myScript,
true
);
}
}
一运行发生了一个错误,根本不弹出窗口,原因就是当Page.ClientScriptRegisterClientScriptBlock已经生效的时候,textbox还没有开始生成呢。所以根本就无办法找到TextBox1
这个时候只是需要修改一下Page.ClientScript.RegisterClientScriptBlock变成Page.ClientScript.RegisterStartupScript
2.Page.ClientScript.RegisterStartupScript()
当您有一个想要在页面加载时启动的 JavaScript 函数时.
RegisterStartupScript 方法的两个可能结构如下:
• RegisterStartupScript (type, key, script)
• RegisterStartupScript (type, key, script, script tag specification)
Page.ClientScript.RegisterStartupScript(
this
.GetType(),
"
MyScript
"
,
"
function AlertHello() { alert('你好,ASP.NET'); }
"
,
true
);
Button1.Attributes[
"
onclick
"
]
=
"
AlertHello()
"
;
Button2.Attributes[
"
onclick
"
]
=
"
AlertHello()
"
;
3. Page.ClientScript.RegisterClientScriptInclude
这个是用来include js 文件的,以前我们都是写个<script type......>个头在html中,现在不需要了
直接在asp.net 页面中书写以下代码,比如我们有个myjs.js 文件
string
myScript
=
"
myjs.js
"
;
page.ClientScript.RegisterClientScriptInclude(
"
mykey
"
,myScript);