使用RenderBeginTag&的意义何在?RenderEndTag [英] What's the point in using RenderBeginTag & RenderEndTag

查看:70
本文介绍了使用RenderBeginTag&的意义何在?RenderEndTag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义控件可以使用此示例来渲染span标签:

Custom controls can use this for example to render out a span tag:

writer.RenderBeginTag(HtmlTextWriterTag.Span);  
writer.Write(Text);  
writer.RenderEndTag();

好吧,为什么我不能这样做:

Ok, why can't I just do this:

writer.Write("<span>");
writer.Write(Text);
writer.Write(</span>");

通过这种方式,我实际上可以看到它,而不是到处读取HtlmlTextWriter标签,并且还可以轻松地对任何标记进行调整,例如向标记添加cs类,id或whaetver.而且,如果您说由于Intellisense打字速度更快,那是使用RenderBegin和RenderEnd的唯一原因吗?情况不多.

this way I can actually see it rather than reading HtlmlTextWriter tags all over the place and also tweak any of the mark-up easily such as adding cs classes, ids, or whaetver to the tags. And if you say it's faster to type because of Intellisense, is that about the only reason to use RenderBegin and RenderEnd? that's not much of a case.

推荐答案

在我工作的地方,我们仅在大型自定义控件集中使用RenderBeginTag()/RenderEndTag()技术,通常避开调用Write(") 直接地.有几个很好的理由:

Here where I work, we use the RenderBeginTag()/RenderEndTag() technique exclusively in our large suite of custom controls, and generally eschew calling Write("") directly. There are several good reasons:

  • 正如Patrick所指出的那样,通过使用RenderBeginTag()/RenderEndTag(),您不会偶然发出名称带有错字的元素.

  • As Patrick noted, by using RenderBeginTag()/RenderEndTag(), you can't accidentally emit elements with typos in their names.

使用RenderBeginTag()/RenderEndTag(),ASP.NET运行时将告诉您是否发出了错误的标记-如果您忘记了以前的RenderBeginTag(),ASP的匹配RenderEndTag().NET将在您的页面上引发异常.这使得拥有一个未封闭的元素真的很困难.

With RenderBeginTag()/RenderEndTag(), the ASP.NET runtime will tell you if you've emitted bad markup --- if you forget a matching RenderEndTag() for a previous RenderBeginTag(), ASP.NET will throw an exception on your page. This makes it really hard to have an unclosed element.

同样,您也不能以这种方式使结束标记不匹配---< div>必须以</div>结尾,并且不能意外地以</span>结尾.

Likewise, you can't mismatch closing tags this way either --- <div> must be ended with </div>, and cannot be accidentally ended with </span>.

AddAttribute()方法允许将复杂的单个元素呈现为逻辑块:该方法添加了适当的CSS类名,该类名正确计算了 value 属性,该方法在那边添加了一个特殊的自定义 foo 属性.可以使用StringBuilder进行这种元素组装,但是它更容易出错.

The AddAttribute() method allows rendering of complex single elements to be divided up into logical chunks: This method adds appropriate CSS classnames, that one correctly computes the value attribute, and that method over there adds a special custom foo attribute. Doing that kind of element assembly with a StringBuilder is possible, but it can be more error-prone.

工具可以自动为您找到可靠的用法.如果我想知道控件中每行发出跨度的代码,例如,我可以搜索HtmlTextWriterTag.Span的用法.搜索< span"更容易出错,并且如果<"不正确,可能会错过一些用法.是与"span"分开写出的(就像在控件中,根据 Enabled在 span a 元素之间来回切换的情况一样标志).

Tools can automatically and reliably find usages for you. If I want to know every line of code in our controls that emits, say, a span, I can just search for usages of HtmlTextWriterTag.Span. Searching on "<span" is a bit more error-prone, and may miss some usages if the "<" is written out separately from the "span" (as would be the case in a control that switches back and forth between a span and an a element depending an Enabled flag).

扩展方法可以扩展AddAttribute()方法,以更复杂的方式添加属性:我们有一个AddNonNullClassnames()扩展方法,该方法可以从多个字符串中的任意一个构造 class 属性传入的值既不为null也不为空,并且证明了在构建仍可以通过CSS完全实现样式的复杂控件方面天赐良机.

Extension methods can expand on the AddAttribute() method to add attributes in more sophisticated ways: We have an AddNonNullClassnames() extension method that constructs the class attribute from any of the multiple strings that are passed in that are neither null nor empty, and it's proven a godsend in building complicated controls that are still fully style-able via CSS.

ASP.NET将根据元素的嵌套深度尝试(尝试)对输出执行适当的缩进.它并不总是成功,但是它的调试结果比将所有HTML发出一行或砸在左列上要好.

ASP.NET will attempt (attempt, I say) to perform proper indentation of the output based on how deeply nested the elements are. It doesn't always succeed, but its result is better for debugging than just having all your HTML emitted on one line, or smashed up against the left column.

(出于记录,我们使用纯CSS布局--- my 手表上没有基于表的布局,谢谢!---这些技术也对此有所帮助.)

(For the record, we use pure-CSS layouts --- no table-based layouts on my watch, thank you! --- and these techniques have helped with that too.)

我本人一开始就对此很抵制---正如您所指出的那样,它又长又罗,、专有,并且来自开源背景,我一开始发现它很臭---但经过三点多年的使用经验,我可以说它使代码更灵活,使某些复杂的操作变得更加容易,并为输出增加了令人惊讶的安全性和整洁度.

I was pretty resistive of this at first myself --- it's long, and wordy, and proprietary, just like you noted, and coming from an open-source background I found it pretty stinky at first --- but after three years of working with it, I can say that it makes the code more flexible, makes certain complex operations easier, and adds a surprising amount of safety and cleanliness to the output.

这篇关于使用RenderBeginTag&amp;的意义何在?RenderEndTag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆