.Net学习笔记 - javascript脚本注册

【ClientScriptManager  篇】 

ClientScriptManager cs = this.ClientScript;// ClientScript是page类里面的
1)

  1. cs.RegisterArrayDeclaration("Hello""1, 2, 3");

    -- 输出一个数组的定义,客户端呈现形式是

  1. <script type="text/javascript">
  2.     var Hello =  new Array(1, 2, 3);
  3. </script>

 2)

  1. cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld""function helloWorld(){alert(1);}"true);

  -- 输出一个代码块,最后一个参数true表示会自动在代码块外围加上<script></script>,客户端呈现形式

  1. <script type="text/javascript">
  2.     function helloWorld(){alert(1);}
  3. </script>

 3)  

  1. cs.RegisterClientScriptInclude("HelloWorld""HelloWorld.js");

  -- 加载一个js文件到页面

 

4)

  1. cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello""World");

  -- 给控件添加一个Attribute,客户端呈现形式是

  1. <script type="text/javascript">
  2.     var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
  3.     Button1.Hello = "World";
  4. </script>

5)

  1. cs.RegisterHiddenField("hello""world");

  -- 输出一个<input type='hidden'>控件,在客户端呈现形式是<input type="hidden" name="hello" id="hello" value="world" />

 

6)

  1. cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld""return window.confirm('Do you really want to submit the form?')");

  -- 添加一段语句,返回一个值,值为true则提交,否则不会提交

 

7)

  1. 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

  1. ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1""alert(1)"true);
  2. ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Page""alert(2)"true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值