【ClientScriptManager 篇】
ClientScriptManager cs = this.ClientScript;// ClientScript是page类里面的
1)
- cs.RegisterArrayDeclaration("Hello", "1, 2, 3");
-- 输出一个数组的定义,客户端呈现形式是
- <script type="text/javascript">
- var Hello = new Array(1, 2, 3);
- </script>
2)
- cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);
-- 输出一个代码块,最后一个参数true表示会自动在代码块外围加上<script></script>,客户端呈现形式
- <script type="text/javascript">
- function helloWorld(){alert(1);}
- </script>
3)
- cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");
-- 加载一个js文件到页面
4)
- cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");
-- 给控件添加一个Attribute,客户端呈现形式是
- <script type="text/javascript">
- var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
- Button1.Hello = "World";
- </script>
5)
- cs.RegisterHiddenField("hello", "world");
-- 输出一个<input type='hidden'>控件,在客户端呈现形式是<input type="hidden" name="hello" id="hello" value="world" />
6)
- cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");
-- 添加一段语句,返回一个值,值为true则提交,否则不会提交
7)
- cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");
-- 输出一个代码块,与RegisterClientScriptBlock的区别是Block在Form开始标记之后,Startup是Form结束标记之前
【ScriptManager 篇】
ScriptManager是用来在异步环境中注册脚本,当然也兼容在同步环境中注册脚本(其实在同步环境中ScriptManager也是自动调用以前ClientScriptManager中的方法),更加包含了ClientScriptManager中所有的脚本注册方法,所以新方法完全可以替代原有方法。
与旧方法的区别主要是多了一个参数,即第一个参数,Control control 或者 Page page
- ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
- ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Page", "alert(2)", true);