Control.RenderChildren(HtmlTextWriter) 方法


将服务器控件子级的内容输出到提供的 HtmlTextWriter 对象,此对象编写将在客户端呈现的内容。Outputs the content of a server control's children to a provided HtmlTextWriter object, which writes the content to be rendered on the client.

protected public:
 virtual void RenderChildren(System::Web::UI::HtmlTextWriter ^ writer);
protected internal virtual void RenderChildren (System.Web.UI.HtmlTextWriter writer);
abstract member RenderChildren : System.Web.UI.HtmlTextWriter -> unit
override this.RenderChildren : System.Web.UI.HtmlTextWriter -> unit
Protected Friend Overridable Sub RenderChildren (writer As HtmlTextWriter)



一个 HtmlTextWriter 对象,用于接收呈现内容。The HtmlTextWriter object that receives the rendered content.


下面的示例重写自定义服务器控件中的 RenderChildren 方法。The following example overrides the RenderChildren method in a custom server control. 它确定当前控件在其 ControlCollection 对象中是否有任何子控件。It determines whether the current control has any child controls in its ControlCollection object. 如果是,它将使用 Count 属性来循环访问集合。If it does, it uses the Count property to iterate through the collection. 在遇到每个子控件时,它使用 RenderControl 方法将父控件及其所有子控件呈现给包含页。As it encounters each child control, it uses the RenderControl method to render the parent control, and all of its child controls, to the containing page.

然后,重写的 Render 方法调用重写的 RenderChildren 方法。The overridden Render method then calls the overridden RenderChildren method.

// Override default implementation to Render children according to needs. 
protected override void RenderChildren(HtmlTextWriter output)
   if (HasControls())
      // Render Children in reverse order.
      for(int i = Controls.Count - 1; i >= 0; --i)

protected override void Render(HtmlTextWriter output)
   output.Write("<br>Message from Control : " + Message);       
   output.Write("Showing Custom controls created in reverse" +
   // Render Controls.
' Override default implementation to Render children according to needs. 
      Protected Overrides Sub RenderChildren(output As HtmlTextWriter)
         If HasControls() Then
            ' Render Children in reverse order.
            Dim i As Integer

            For i = Controls.Count - 1 To 0 Step -1

         End If
      End Sub
      Protected Overrides Sub Render(output As HtmlTextWriter)
         output.Write(("<br>Message from Control : " + Message))
         output.Write(("Showing Custom controls created in reverse" + "order"))
         ' Render Controls.
      End Sub
   End Class


此方法通知 ASP.NET 在页面上呈现任何活动服务器页(ASP)代码。This method notifies ASP.NET to render any Active Server Pages (ASP) code on the page. 如果该页不存在任何 ASP 代码,则此方法将呈现服务器控件的任何子控件。If no ASP code exists on the page, this method renders any child controls for the server control. 此方法由 Render 方法调用。This method is called by the Render method.