ASP.NET中如何向页面写入JavaScript脚本内容

使用Ajax的方法在后置代码(.cs文件中)向页面写入JavaScript脚本的方法,

虽然Response.Write("<script>脚本内容</script>");的方式也可以做到向页面写入脚本代码,

但是脚本输出的内容多在网页显示的最上方,用以下方法可以将脚本内容写在你想要的任意地方。

 

 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.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> " );

       

   以上为脚本注册的用法,以下是一点注释:


        //RegisterArrayDeclaration效果为定义一个数组
        //---------产生脚本为:var Hello =  new Array(1, 2, 3);
        //RegisterClientScriptBlock效果产生一个函数,紧跟着源代码中的form标签
        //---------产生脚本为:function helloWorld(){alert(1);}
        //RegisterClientScriptInclude引用外部js文件
        //---------产生脚本为:<script src="HelloWorld.js" type="text/javascript"></script>
        //RegisterHiddenField产生一个名为hello值为world的隐藏控件
        //---------产生脚本为:<input type="hidden" name="hello" id="hello" value="world" />
        //RegisterOnSubmitStatement
        //---------产生脚本为:function WebForm_OnSubmit() {return window.confirm('Do you really want to submit the form?');return true;}
        //RegisterStartupScript产生一个函数,位置紧跟着结束的form标签
        //---------产生脚本为:<script>alert('The page has loaded!')</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值