WebControl创建中Render和RenderControl有什么区别?

发布于 2024-08-09 01:34:49 字数 154 浏览 4 评论 0原文

我最近学会了编写自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请爱~陌生人 2024-08-16 01:34:49

这是一篇很好的读物:
Render 与 RenderControl

链接摘录:

Render 方法启用
控制器通过写入来呈现自身
HTML 输出到响应流。
这传递了对
HTMLTextWriter对象,可以写入
直接到响应流。这
导出时应使用方法
直接从控制。

RenderControl方法
通过页面来呈现每个个体
控制。它允许消费者
你的类来渲染它,你可以
使用它来呈现子控件,如果
您正在使用复合控件。
另外,需要注意的一点是,这
如果可见性,则不会被调用
控件上的属性设置为
错误。

Here's a good read:
Render vs RenderControl

Excerpt from links:

The Render method enables the
controller to render itself by writing
HTML output to the response stream.
This passes a reference to the
HTMLTextWriter object, which can write
directly to the response stream. This
method should be used when you derive
directly from control.

The RenderControl method is used
by the page to render each individual
control. It allows the consumer of
your class to render it, and you can
use it to render the child controls if
you're using a composite control.
Also, one thing to note is that this
will not be invoked if the visibility
property on the control is set to
false.

盗心人 2024-08-16 01:34:49

Render 是一种受保护的方法,这意味着只有派生类才能访问它。它在事件生命周期内调用,不应在代码中显式调用。

RenderControl 是一个公共方法,允许您在需要时调用 Render 方法。例如,您可以在自定义控件中使用它,您将控件存储在 ControlsCollection 中,但希望将它们呈现在表中自己的单元格中。例如:

   writer.Write(""):
   foreach(Control ctl in Controls)
   {
      writer.Write("")
      ctl.RenderControl(writer);
      writer.Write("");
   }
   writer.Write("");

您应该始终覆盖 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:

   writer.Write(""):
   foreach(Control ctl in Controls)
   {
      writer.Write("")
      ctl.RenderControl(writer);
      writer.Write("");
   }
   writer.Write("");

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.

夜灵血窟げ 2024-08-16 01:34:49

如果现有控件可以提供您想要的内容,则使用 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.

薯片软お妹 2024-08-16 01:34:49

RenderControl用于页面渲染子控件。渲染允许单个控件渲染自身。

RenderControl is used for the page to render child controls. Render allows an individual control to render itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文