asp.net ajax--脚本注册

 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 );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值