惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

博客园 - IT爱好者

C# 自定义打印 C# 打印,不显示打印进度对话框 如何解决迅雷插件导致IE10崩溃的问题 ASP.NET MVC 使用Jquery Uploadify 在非IE浏览器下Http Error的解决方案 c#操作access,update语句不执行的解决办法 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用 服务器“**”上的MSDTC不可用的解决办法 通过Package Manager Console 向VS2010安装 EFCodeFirst 删除SVN遗留的无用文件 解决"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本" 修改IIS默认的localhost名称 IIS服务器应用程序不可用的解决办法 使用XML与远程服务器进行交互 小米手机指令大全 Java与.NET DES加密解密互转 获取执行SQL语句的返回结果 Oracle自增列创建方法 ASP.net中用JSON序列化对象 android Listview 拖动时背景为黑色问题
ASP.NET中如何向页面写入JavaScript脚本内容
IT爱好者 · 2011-08-04 · via 博客园 - IT爱好者

使用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>