实现@using{}代码块
来源:互联网 发布:耳机频响测试软件 编辑:程序博客网 时间:2024/05/10 22:21
前几天,我要写个插件,放在asp.net mvc的view上。这需要写一些扩展HtmlHelper功能的方法。这些方法的一个参数是一段javascript代码。
假如这个参数类型是字符型,当然很简单了。问题是,将一段js代码放在字符串里,写的时候会很不方便,没有VS编辑器的智能提示;同时,JS里面有字符串的话,还要避免与外层的单/双引号冲突。
能不能像razor那样,直接将html或js代码嵌在c#代码里呢?比如像这样:
@using(BeginForm()){ <div>呵呵,我是html代码,我在using代码块里面。</div>}
这段代码输出的浏览器以后,会变成:
<form><div>呵呵,我是html代码,我在using代码块里面。</div></form>
就是说,系统会自动在头尾加上”<form>”的开始和结束标记。它是怎么做到的?
其实,凡支持using代码块的对象,都要实现IDisposable接口。在using结尾处,也就是对象结束时,调用了Dispose方法。处理结束标记,就放在Dispose方法里面!
经过一番努力,并研读了asp.net mvc的源代码(听起来挺高大上),书写代码如下:
public class TableHeadSorterScript : IDisposable { private readonly TextWriter _writer; private readonly string _endscript; private bool _disposed; public TableHeadSorterScript(HtmlHelper html, string endscript = ");") { this._writer = html.ViewContext.Writer; this._endscript = endscript; } public void Dispose() { Dispose(true /* disposing */); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposed = true; _writer.Write(_endscript); } } public void EndScript() { Dispose(true); } } public static class TableHeadSortHelper { static readonly string beginscript = @"$(function () { tableHeadSort_init("; static readonly string endscript = @" );});"; /* --------------------------------------------------------- 输出自动初始化表头排序插件的JS脚本 此脚本在页面装载完成后触发;注意用ajax动态加载页面情况下此脚本无效 --------------------------------------------------------- e.g. @using (Html.BeginTableHeadSortAutoInit()) { @:function(url) { location.href = url; } } */ public static TableHeadSorterScript BeginTableHeadSortAutoInit(this HtmlHelper helper) { helper.ViewContext.Writer.Write(beginscript); TableHeadSorterScript script = new TableHeadSorterScript(helper,endscript); return script; } }
放在view上
@using (Html.BeginTableHeadSortAutoInit()){ @:function(url) { location.href = url; }}
,输出到浏览器,会看到:
$(function () { tableHeadSort_init( function (url) { location.href = url; } );});
0 0
- 实现@using{}代码块
- Using 块
- 使用synchronized实现同步代码块
- iOS复用代码块实现
- eclipse中实现代码块的折叠
- csdn代码块如何是实现?
- 实现二进制代码块的复制
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- 代码块
- android项目调用c接口
- 阿里云yum不升级内核的解决方案
- POJ 2985 The k-th Largest Group [并差集+treap]
- JavaScript获取当前根目录
- android github 资源地址
- 实现@using{}代码块
- 电话通讯录用到的 Rui
- 仿QQ对话列表滑动删除与置顶的原理及实现
- poj1328
- 利用CGMutablePathRef制作画板涂鸦
- SQL增删改查语句
- POJ 2774 long long message(后缀数组求最长公共子串)
- 服务端控件
- 查看苹果是否感染Xcode SHA1值方法