\");的方式也可以做到向页面写入脚本代码, 但是脚本…","url":"https://www.cnblogs.com/shunyao8210/archive/2011/08/04/2127491.html","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.cnblogs.com/shunyao8210/archive/2011/08/04/2127491.html"},"image":"https://assets.cnblogs.com/logo.svg","publisher":{"@type":"Organization","name":"惯性聚合","logo":{"@type":"ImageObject","url":"https://juhe.plus/apple-touch-icon.png"}},"author":{"@type":"Person","name":"IT爱好者"},"datePublished":"2011-08-04T07:58:00.000Z"}]}
惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
爱范儿
爱范儿
Y
Y Combinator Blog
T
Tor Project blog
V
Visual Studio Blog
U
Unit 42
B
Blog RSS Feed
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
G
Google Developers Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
博客园_首页
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
P
Proofpoint News Feed
Jina AI
Jina AI
博客园 - 【当耐特】
S
Security @ Cisco Blogs
I
Intezer
MyScale Blog
MyScale Blog
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
腾讯CDC
T
Tenable Blog
A
Arctic Wolf
T
Threat Research - Cisco Blogs
S
Securelist
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
C
Check Point Blog
NISL@THU
NISL@THU
Microsoft Security Blog
Microsoft Security Blog
V
Vulnerabilities – Threatpost

博客园 - 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>