Control.Render(HtmlTextWriter) 方法


将服务器控件内容发送到给定的 HtmlTextWriter 对象,该对象可编写将在客户端呈现的内容。Sends server control content to a provided HtmlTextWriter object, which writes the content to be rendered on the client.

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



用于接收服务器控件内容的 HtmlTextWriter 对象。The HtmlTextWriter object that receives the server control content.


下面的代码示例演示如何重写 Render 方法。The following code example demonstrates overriding the Render method. HasControls 方法用于确定服务器控件是否具有存储在其 ControlCollection 对象中的任何子控件,该对象可通过 Control.Controls 属性进行访问。The HasControls method is used to determine whether the server control has any child controls stored in its ControlCollection object, which is accessible through the Control.Controls property. 如果 HasControls 返回 true,并且集合中的第一个服务器控件是文本文本,则文本文本将追加到 HTML 字符串。If HasControls returns true and the first server control in the collection is literal text, then the literal text is appended to an HTML string.

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter output) {
    if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
        output.Write("<H2>Your Message: " + ((LiteralControl) Controls[0]).Text + "</H2>");
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub Render(Output As HtmlTextWriter)
    If HasControls() And TypeOf Controls(0) Is LiteralControl
        Dim Ctrl As LiteralControl = CType(Controls(0), LiteralControl)
        Output.Write("<H2>Your Message: " & Ctrl.Text & "</H2>")
    End If
End Sub


开发自定义服务器控件时,可以重写此方法,以生成 ASP.NET 页的内容。When developing custom server controls, you can override this method to generate content for an ASP.NET page.