Control.Render(HtmlTextWriter) Control.Render(HtmlTextWriter) Control.Render(HtmlTextWriter) Control.Render(HtmlTextWriter) Method


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

protected public:
 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 Friend Overridable Sub Render (writer As HtmlTextWriter)


HtmlTextWriter HtmlTextWriter HtmlTextWriter 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.