用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:区别:
c#
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果如下:
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
2.使用Page.ClientScript.RegisterStartupScript
RegisterStartupScript 方法与RegisterClientScriptBlock方法最大的不同是:RegisterStartupScript 把script放置在 ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部。
如果你的页面中有如下代码:
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
c#
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
此页面运行时会报错,原因是JavaScript function先于text box被安放于浏览器。因此JavaScript function找不到TextBox1。
c#
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这段代码把JavaScript function放置于ASP.NET page底部,因此JavaScript运行时它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
许多开发者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的JavaScript。
c#
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
![ASP.NET2.0中RegisterClientScriptBloc<wbr>k <wbr>怎样用 ASP.NET2.0中RegisterClientScriptBlock <wbr>怎样用](http://zhhui.cnblogs.com/Images/OutliningIndicators/None.gif)
这将在ASP.NET页面产生如下结构:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
RegisterClientScriptBlock() 在 Web 窗体的开始处(紧接着 <form runat="server">标识之后)发送脚本块,而 RegisterStartupScript() 在 Web 窗体的结尾处(在 </form>标识之前)发送脚本块。
RegisterStartupScript() 用于添加要在加载页面后运行的脚本块,通过这种方法添加的脚本块位于 Web 窗体的结尾处,因为必须在脚本运行前定义脚本要修改的 HTML 元素。也就是说,如果您要使用客户端脚本将焦点设置到文本框,必须确保文本框的 HTML 标记位于设置该文本框的焦点的脚本之前。
RegisterClientScriptBlock() 方法用于为响应客户端事件而执行的脚本代码。通过此方法发送的脚本块位于 Web 页面的开始处,因为这种方法不要求将脚本块置于所有 HTML 元素之后,即它一般不使用文档内自定义的控件。
如果你仅仅是注册一些函数,这两者的效果是一样的。但如果你要注册一些全局的脚本,如定义一些全局变量并赋值,那么它在HTML中的先后位置可能就比较重要,此时就应该用RegisterStartupScript保证脚本能在前面被执行。
以下是该方法的一个使用实例(见msdn):
<html>
<head>
<script language="VB" runat="server">
Sub Page_Load( sender as Object,e as EventArgs)
'Form the script that is to be registered at client side.
Dim scriptString as String = "<script language=JavaScript> function DoClick() {"
scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<"
scriptString += "/"
scriptString += "script>"
If(Not IsClientScriptBlockRegistered("clientScript")) RegisterClientScriptBlock("clientScript", scriptString)
End If
End Sub
</script>
</head>
<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">
<input type="text" id="show" style="width=200">
<input type="button" value="ClickMe" οnclick="DoClick()">
</form>
</body>
</html>