1.禁止使用Response.Write方法
2.使用Framework所支持的方法---对象ClientScriptManager
脚本文件HelloWorld.js
(1) 在code-behand中生成对象:ClientScriptManager cs=this.ClientScript;
(2) 注册脚
ClientScriptManager cs=this.ClientScript;
cs.RegisterArrayDeclaration("Hello", "1, 2, 3");
cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function 

helloWorld()...{alert(1);}", true);
cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");
//cs.RegisterClientScriptResource();
cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");
cs.RegisterHiddenField("hello", "world");
cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return 
window.confirm('Do you really want to submit the form?')");
cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The 
page has loaded!')</script>");
(3) 对应的脚本
<script type="text/javascript">
<!--
var Hello = new Array(1, 2, 3);
// -->
</script>
<script type="text/javascript">
<!--
function helloWorld()...{alert(1);}// -->
</script>
<script src="HelloWorld.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
var Button1 = document.all ? document.all["Button1"] : 
document.getElementById("Button1");
Button1.Hello = "World";
// -->
</script>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" 
value="/wEWAgKP1ajkCQKM54rGBmM4dY9RujzZrs2rzIpU3LPTwB3F" />
</div>
<script type="text/javascript">
<!--
function WebForm_OnSubmit() ...{
return window.confirm('Do you really want to submit the form?');
return true;
}
// -->
</script>
<script>alert('The page has loaded!')</script>
(4) 在异步更新的环境中注册脚本,使用ScriptManager对应的静态方法
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "UpdatePanel2", "alert(2)", true);
本文介绍如何在 ASP.NET 应用程序中利用 ClientScriptManager 对象进行客户端脚本管理,包括注册脚本块、外部脚本文件、启动脚本等,以及在异步更新场景下的应用。
3234

被折叠的 条评论
为什么被折叠?



