C#向前台页面中输出JS代码

[csharp]  view plain copy
  1.     //需要注意的是  
  2.     //当向页面输出带有相同的Type和Key的的参数时可以避免重复输出  
  3. ------------------------------------------------------------------------------------------------------------------------------------------------      
  4.     //在UpdatePanel以外按钮输出JS方法  
  5.   
  6.      ClientScriptManager cs = this.ClientScript;  
  7.   
  8.         cs.RegisterArrayDeclaration("Hello""1, 2, 3");  
  9.         //输出结果:  
  10.         //<script type="text/javascript">  
  11.         //var Hello =  new Array(1, 2, 3);  
  12.         //</script>  
  13.   
  14.         cs.RegisterClientScriptInclude("HelloWorld""HelloWorld.js");  
  15.         //输出结果:  
  16.         //<script src="HelloWorld.js" type="text/javascript"></script>  
  17.   
  18.          cs.RegisterClientScriptResource(  
  19.   
  20.         cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello""World");  
  21.         //输出结果:  
  22.         //向Button1按钮注册一个Hello的属性  
  23.         //<script type="text/javascript">  
  24.         //var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");  
  25.         //Button1.Hello = "World";  
  26.         //</script>  
  27.   
  28.         cs.RegisterHiddenField("hello""world");  
  29.         //输出结果:  
  30.         //<input type="hidden" name="hello" id="hello" value="world" />  
  31.   
  32.         cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld""return window.confirm('Do you really want to submit the form?')");  
  33.         //输出结果:  
  34.         //(OnSubmit)事件,点击按钮指出提示框  
  35.         //<script type="text/javascript">  
  36.         //function WebForm_OnSubmit() {  
  37.         //return window.confirm('Do you really want to submit the form?');  
  38.         //return true;  
  39.         //}  
  40.         //</script>  
  41.   
  42.         cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld""function helloWorld(){alert(1);}"true);  
  43.         //输出结果:  
  44.         //在页面顶部  
  45.         //<script type="text/javascript">  
  46.         //function helloWorld(){alert(1);}  
  47.         //</script>  
  48.   
  49.         cs.RegisterStartupScript(this.GetType(), "HelloWorld""<script>alert('The page has loaded!')</script>");  
  50.         //输出结果:  
  51.         //在页面底部  
  52.         //<script>alert('The page has loaded!')</script>  
  53.   
  54. --------------------------------------------------------------------------------------------------------------------------------------------  
  55.     //在UpdatePanel以内按钮输出JS方法  
  56.   
  57.         //为某个UpdatePanel输出JS脚本  
  58.         //注:只有当按钮所在的UpdatePanel更新时此方法生效  
  59.         ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1""alert(1)"true);  
  60.   
  61.     //为当前页面输出JS脚本  
  62.         //注:无论当前UpdatePanel是否更新都向页面输出JS脚本  
  63.         ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "UpdatePanel2""alert(2)"true);     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值