WebControl创建中Render和RenderControl有什么区别?
我最近学会了编写自己的 WebControl,但我仍然对 Render 和 RenderControl 之间的区别感到模糊。我一开始使用 Render,但在某个时候我开始使用 RenderControl,现在我只使用它。如果我养成了一个坏习惯,我想在我开始采取自己的方式之前将其消灭在萌芽状态。
I've recently learned to write my own WebControls, but I'm still hazy on the difference between Render and RenderControl. I started out using Render, but at some point I started using RenderControl, which I now use exclusively. If I'm forming a bad habit I'd like to nip it in the bud now before I get set in my ways.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一篇很好的读物:
Render 与 RenderControl
链接摘录:
Here's a good read:
Render vs RenderControl
Excerpt from links:
Render 是一种受保护的方法,这意味着只有派生类才能访问它。它在事件生命周期内调用,不应在代码中显式调用。
RenderControl 是一个公共方法,允许您在需要时调用 Render 方法。例如,您可以在自定义控件中使用它,您将控件存储在 ControlsCollection 中,但希望将它们呈现在表中自己的单元格中。例如:
您应该始终覆盖 Render,而不是 RenderControl,因为它是在事件生命周期中调用的(正如我已经说过的)
(来自 http://forums.asp.net/t/909220.aspx/1,抱歉,我的答案只是复制粘贴,但我没有看到太多点重写如此清晰的解释)
我仍然不明白为什么 MS 不简单地将 Render() 设为公共方法。为什么需要单独的 RenderControl() ?迄今为止给出的所有答案都缺少其推理。第一个能够解释它的人会获得闪亮的赞成票。
Render is a protected method, meaning that only derived Classes can access it. It is called within the Event Lifecycle, and shouldn't be explicitly called in your code.
RenderControl is a public method that allows you to call the Render method when you want. You would use it in, say, a Custom Control where you store Controls in the ControlsCollection but want to Render them in their own cell in a table. For example:
You should always override Render, and not RenderControl, as it is called in the Event Lifecycle (as I have already said)
(from http://forums.asp.net/t/909220.aspx/1, sorry my answer is just a copy-and-paste from that, but I didn't see much point rewriting such a crystal clear explanation)
I still don't understand why MS didn't simply make Render() a public method. Why is a separate RenderControl() necessary? The reasoning for that is missing from all the answers given so far. The first person who can explain it gets a shiny upvote.
如果现有控件可以提供您想要的内容,则使用 RenderControl,以便您可以利用其呈现方式。
如果您需要完全控制 html 的渲染方式,则可以使用渲染。
您当然可以将两者结合起来,以最佳地利用您的编程时间...
编辑
有关此问题的更好解释以及何时使用的要点,请参阅 来自 mhenry1384 的答案。
RenderControl is used if there is an existing control that provides what you want so you can take advantage of how that is rendered.
Render is used if you need to take full control over how the html is rendered.
You can of course combine the two for the most optimal use of your programming time...
EDIT
For a better explanation on this, and excellent point to what to use when, see the answer from mhenry1384.
RenderControl用于页面渲染子控件。渲染允许单个控件渲染自身。
RenderControl is used for the page to render child controls. Render allows an individual control to render itself.