代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
JavaScript脚本输出类
作者:
/ 发布于
2010/12/30
/
584
ASP.NET页面上,通过后端代码输出Javascript脚本,一般可以使用 Page.ClientManager ,将拼凑的整个脚本块一起输出。然而通过下面这个类,可以实现以另外的方式输出Javascript脚本,他提供了友好的格式化功能,提供了“{”,“}”匹配检查。
<div> <div class="container"> <div class="line number1 index0 alt2">"c# keyword">internal "c# keyword">class "c# plain">JavaScriptWriter </div> <div class="line number2 index1 alt1">"c# plain">{ </div> <div class="line number3 index2 alt2">"c# spaces">"c# keyword">public "c# plain">JavaScriptWriter() {} </div> <div class="line number4 index3 alt1"></div> <div class="line number5 index4 alt2">"c# spaces">"c# preprocessor">#region 变量区 </div> <div class="line number6 index5 alt1"></div> <div class="line number7 index6 alt2">"c# spaces">"c# keyword">private "c# plain">StringBuilder sb = "c# keyword">new "c# plain">StringBuilder(); </div> <div class="line number8 index7 alt1">"c# spaces">"c# keyword">private "c# keyword">int "c# plain">currIndent = 0; </div> <div class="line number9 index8 alt2">"c# spaces">"c# keyword">private "c# keyword">int "c# plain">openBlocks = 0; </div> <div class="line number10 index9 alt1">"c# spaces">"c# keyword">private "c# keyword">bool "c# plain">format = "c# keyword">false"c# plain">; </div> <div class="line number11 index10 alt2"></div> <div class="line number12 index11 alt1">"c# spaces">"c# preprocessor">#endregion </div> <div class="line number13 index12 alt2"></div> <div class="line number14 index13 alt1">"c# spaces">"c# preprocessor">#region 方法区 </div> <div class="line number15 index14 alt2"></div> <div class="line number16 index15 alt1">"c# spaces">"c# color1">/// <summary> </div> <div class="line number17 index16 alt2">"c# spaces">"c# color1">/// 在书入到页面时是否需要格式 </div> <div class="line number18 index17 alt1">"c# spaces">"c# color1">/// </summary> </div> <div class="line number19 index18 alt2">"c# spaces">"c# color1">/// <param name="Formatted">需要格式?</param> </div> <div class="line number20 index19 alt1">"c# spaces">"c# keyword">public "c# plain">JavaScriptWriter("c# keyword">bool "c# plain">Formatted) </div> <div class="line number21 index20 alt2">"c# spaces">"c# plain">{ </div> <div class="line number22 index21 alt1">"c# spaces">"c# plain">format = Formatted; </div> <div class="line number23 index22 alt2">"c# spaces">"c# plain">} </div> <div class="line number24 index23 alt1"></div> <div class="line number25 index24 alt2">"c# spaces">"c# color1">/// <summary> </div> <div class="line number26 index25 alt1">"c# spaces">"c# color1">/// 当前的缩进层次 </div> <div class="line number27 index26 alt2">"c# spaces">"c# color1">/// </summary> </div> <div class="line number28 index27 alt1">"c# spaces">"c# keyword">public "c# keyword">int "c# plain">Indent </div> <div class="line number29 index28 alt2">"c# spaces">"c# plain">{ </div> <div class="line number30 index29 alt1">"c# spaces">"c# keyword">get "c# plain">{ "c# keyword">return "c# plain">currIndent; } </div> <div class="line number31 index30 alt2">"c# spaces">"c# keyword">set "c# plain">{ currIndent = value; } </div> <div class="line number32 index31 alt1">"c# spaces">"c# plain">} </div> <div class="line number33 index32 alt2"></div> <div class="line number34 index33 alt1">"c# spaces">"c# color1">/// <summary> </div> <div class="line number35 index34 alt2">"c# spaces">"c# color1">/// 新增一行javascript代码 </div> <div class="line number36 index35 alt1">"c# spaces">"c# color1">/// </summary> </div> <div class="line number37 index36 alt2">"c# spaces">"c# color1">/// <param name="parts">代码字串的数组</param> </div> <div class="line number38 index37 alt1">"c# spaces">"c# keyword">public "c# keyword">void "c# plain">AddLine("c# keyword">params "c# keyword">string"c# plain">[] parts) </div> <div class="line number39 index38 alt2">"c# spaces">"c# plain">{ </div> <div class="line number40 index39 alt1">"c# spaces">"c# comments">// 如果有格式设置,则加入缩进的行 </div> <div class="line number41 index40 alt2">"c# spaces">"c# keyword">if "c# plain">(format) </div> <div class="line number42 index41 alt1">"c# spaces">"c# keyword">for "c# plain">("c# keyword">int "c# plain">i=0; i < currIndent; i++) </div> <div class="line number43 index42 alt2">"c# spaces">"c# plain">sb.Append("c# string">"\t""c# plain">); </div> <div class="line number44 index43 alt1"></div> <div class="line number45 index44 alt2">"c# spaces">"c# keyword">foreach "c# plain">("c# keyword">string "c# plain">part "c# keyword">in "c# plain">parts) </div> <div class="line number46 index45 alt1">"c# spaces">"c# plain">sb.Append(part); </div> <div class="line number47 index46 alt2"></div> <div class="line number48 index47 alt1">"c# spaces">"c# keyword">if "c# plain">(format) </div> <div class="line number49 index48 alt2">"c# spaces">"c# plain">sb.Append(Environment.NewLine); </div> <div class="line number50 index49 alt1">"c# spaces">"c# keyword">else</div> <div class="line number51 index50 alt2">"c# spaces">"c# keyword">if "c# plain">(parts.Length > 0) </div> <div class="line number52 index51 alt1">"c# spaces">"c# plain">sb.Append("c# string">" ""c# plain">); </div> <div class="line number53 index52 alt2">"c# spaces">"c# plain">} </div> <div class="line number54 index53 alt1"></div> <div class="line number55 index54 alt2">"c# spaces">"c# color1">/// <summary> </div> <div class="line number56 index55 alt1">"c# spaces">"c# color1">/// 输入"{",并使层次缩进一层 </div> <div class="line number57 index56 alt2">"c# spaces">"c# color1">/// </summary> </div> <div class="line number58 index57 alt1">"c# spaces">"c# keyword">public "c# keyword">void "c# plain">OpenBlock() </div> <div class="line number59 index58 alt2">"c# spaces">"c# plain">{ </div> <div class="line number60 index59 alt1">"c# spaces">"c# plain">AddLine("c# string">"{""c# plain">); </div> <div class="line number61 index60 alt2">"c# spaces">"c# plain">currIndent++; </div> <div class="line number62 index61 alt1">"c# spaces">"c# plain">openBlocks++; </div> <div class="line number63 index62 alt2">"c# spaces">"c# plain">} </div> <div class="line number64 index63 alt1"></div> <div class="line number65 index64 alt2">"c# spaces">"c# color1">/// <summary> </div> <div class="line number66 index65 alt1">"c# spaces">"c# color1">/// 输入"{",并使层次扩展一层 </div> <div class="line number67 index66 alt2">"c# spaces">"c# color1">/// </summary> </div> <div class="line number68 index67 alt1">"c# spaces">"c# keyword">public "c# keyword">void "c# plain">CloseBlock() </div> <div class="line number69 index68 alt2">"c# spaces">"c# plain">{ </div> <div class="line number70 index69 alt1">"c# spaces">"c# comments">// 检查一个function有没有"{" </div> <div class="line number71 index70 alt2">"c# spaces">"c# keyword">if "c# plain">(openBlocks < 1) </div> <div class="line number72 index71 alt1">"c# spaces">"c# keyword">throw "c# keyword">new "c# plain">InvalidOperationException("c# string">"在调用JavaScriptWriter.CloseBlock()时没有先前的JavaScriptWriter.OpenBlock()调用""c# plain">); </div> <div class="line number73 index72 alt2"></div> <div class="line number74 index73 alt1">"c# spaces">"c# plain">currIndent--; </div> <div class="line number75 index74 alt2">"c# spaces">"c# plain">openBlocks--; </div> <div class="line number76 index75 alt1">"c# spaces">"c# plain">AddLine("c# string">"}""c# plain">); </div> <div class="line number77 index76 alt2">"c# spaces">"c# plain">} </div> <div class="line number78 index77 alt1"></div> <div class="line number79 index78 alt2">"c# spaces">"c# color1">/// <summary> </div> <div class="line number80 index79 alt1">"c# spaces">"c# color1">/// 加入注解的(为javascript加入注解) </div> <div class="line number81 index80 alt2">"c# spaces">"c# color1">/// </summary> </div> <div class="line number82 index81 alt1">"c# spaces">"c# color1">/// <param name="CommentText">注解的字串数组.</param> </div> <div class="line number83 index82 alt2">"c# spaces">"c# keyword">public "c# keyword">void "c# plain">AddCommentLine("c# keyword">params "c# keyword">string"c# plain">[] CommentText) </div> <div class="line number84 index83 alt1">"c# spaces">"c# plain">{ </div> <div class="line number85 index84 alt2">"c# spaces">"c# keyword">if "c# plain">(format) </div> <div class="line number86 index85 alt1">"c# spaces">"c# plain">{ </div> <div class="line number87 index86 alt2">"c# spaces">"c# keyword">for "c# plain">("c# keyword">int "c# plain">i=0; i < currIndent; i++) </div> <div class="line number88 index87 alt1">"c# spaces">"c# plain">sb.Append("c# string">"\t""c# plain">); </div> <div class="line number89 index88 alt2"></div> <div class="line number90 index89 alt1">"c# spaces">"c# plain">sb.Append ("c# string">"// ""c# plain">); </div> <div class="line number91 index90 alt2"></div> <div class="line number92 index91 alt1">"c# spaces">"c# keyword">foreach "c# plain">("c# keyword">string "c# plain">part "c# keyword">in "c# plain">CommentText) </div> <div class="line number93 index92 alt2">"c# spaces">"c# plain">sb.Append(part); </div> <div class="line number94 index93 alt1"></div> <div class="line number95 index94 alt2">"c# spaces">"c# plain">sb.Append(Environment.NewLine); </div> <div class="line number96 index95 alt1">"c# spaces">"c# plain">} </div> <div class="line number97 index96 alt2">"c# spaces">"c# plain">} </div> <div class="line number98 index97 alt1"></div> <div class="line number99 index98 alt2">"c# spaces">"c# color1">/// <summary> </div> <div class="line number100 index99 alt1">"c# spaces">"c# color1">/// 转换开发和结束的javascript的标记,并在中间加入已加入的javascrpt的代码 </div> <div class="line number101 index100 alt2">"c# spaces">"c# color1">/// </summary> </div> <div class="line number102 index101 alt1">"c# spaces">"c# color1">/// <returns>返回标准的javascript代码</returns> </div> <div class="line number103 index102 alt2">"c# spaces">"c# keyword">public "c# keyword">override "c# keyword">string "c# plain">ToString() </div> <div class="line number104 index103 alt1">"c# spaces">"c# plain">{ </div> <div class="line number105 index104 alt2">"c# spaces">"c# keyword">if "c# plain">(openBlocks > 0) </div> <div class="line number106 index105 alt1">"c# spaces">"c# keyword">throw "c# keyword">new "c# plain">InvalidOperationException("c# string">"JavaScriptWriter: 没有相应的关闭标识""c# plain">); </div> <div class="line number107 index106 alt2"></div> <div class="line number108 index107 alt1">"c# spaces">"c# keyword">return "c# plain">String.Format( </div> <div class="line number109 index108 alt2">"c# spaces">"c# string">"<script language=\"javascript\" type=\"text/javascript\">{0}{1}</script>""c# plain">, </div> <div class="line number110 index109 alt1">"c# spaces">"c# plain">Environment.NewLine, </div> <div class="line number111 index110 alt2">"c# spaces">"c# plain">sb </div> <div class="line number112 index111 alt1">"c# spaces">"c# plain">); </div> <div class="line number113 index112 alt2">"c# spaces">"c# plain">} </div> <div class="line number114 index113 alt1"></div> <div class="line number115 index114 alt2">"c# spaces">"c# preprocessor">#endregion </div> <div class="line number116 index115 alt1">"c# plain">}</div> </div> </div>
试试其它关键字
脚本输出
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3