使用Ajax的方法在后置代码(.cs文件中)向页面写入JavaScript脚本的方法,
虽然Response.Write("<script>脚本内容</script>");的方式也可以做到向页面写入脚本代码,
但是脚本输出的内容多在网页显示的最上方,用以下方法可以将脚本内容写在你想要的任意地方。
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>