WebPartChrome.RenderPartContents(HtmlTextWriter, WebPart) 方法


呈现 WebPart 控件的主要内容区域,不包括页眉和页脚。Renders the main content area of a WebPart control, excluding the header and footer.

 virtual void RenderPartContents(System::Web::UI::HtmlTextWriter ^ writer, System::Web::UI::WebControls::WebParts::WebPart ^ webPart);
protected virtual void RenderPartContents (System.Web.UI.HtmlTextWriter writer, System.Web.UI.WebControls.WebParts.WebPart webPart);
abstract member RenderPartContents : System.Web.UI.HtmlTextWriter * System.Web.UI.WebControls.WebParts.WebPart -> unit
override this.RenderPartContents : System.Web.UI.HtmlTextWriter * System.Web.UI.WebControls.WebParts.WebPart -> unit
Protected Overridable Sub RenderPartContents (writer As HtmlTextWriter, webPart As WebPart)



用于接收 webPart 内容的 HtmlTextWriterThe HtmlTextWriter that receives the webPart content.


当前正在呈现的控件。The control currently being rendered.


下面的代码示例演示如何使用 RenderPartContents 方法。The following code example demonstrates use of the RenderPartContents method. 有关运行此示例所需的完整代码,请参阅类概述主题的 "示例" 部分 WebPartChromeFor the full code required to run the example, see the Example section of the WebPartChrome class overview topic.

下面的代码示例部分演示如何重写 RenderPartContents 方法。The following section from the code example demonstrates how to override the RenderPartContents method. 重写的方法执行两个任务来自定义控件体的呈现 WebPartThe overridden method does two things to customize the rendering for the body of the WebPart control. 首先,它会检查以确定控件当前是否处于选中状态,如果是,则写出一个字符串并且不呈现内容。First, it checks to determine whether the control is currently selected, and if so, writes out a string and does not render the contents. 第二种情况下,如果未选择控件,并且控件的区域的类型为 MyZone ,则该方法将呈现控件。Second, if the control is not selected, and if the control's zone is of type MyZone, the method renders the control. 如果您想要确保自定义 WebPartChrome 类只可用于呈现 WebPart 特定类型 WebPartZone 区域中的控件,而这些控件设计为包含 WebPartChrome 对象,则可以使用后一种检查。This latter check could be used if you wanted to ensure that a custom WebPartChrome class can be used only to render WebPart controls in a specific type of WebPartZone zone that is designed to contain the WebPartChrome object.

protected override void RenderPartContents(HtmlTextWriter writer, 
  WebPart part)

    if (part == this.WebPartManager.SelectedWebPart)
      HttpContext.Current.Response.Write("<span>Not rendered</span>");
      if(this.Zone.GetType() == typeof(MyZone))
Protected Overrides Sub RenderPartContents _
  (ByVal writer As HtmlTextWriter, ByVal part As WebPart)

  If part Is Me.WebPartManager.SelectedWebPart Then
    HttpContext.Current.Response.Write("<span>Not rendered</span>")
    If (Me.Zone.GetType() Is GetType(MyZone)) Then
    End If
  End If

End Sub

如果您在浏览器中加载网页,您可以看到,每个控件的内容都是正常呈现的。If you load the Web page in a browser, you can see that the content of each control is rendered normally. 如果通过在 "显示模式" 下拉列表控件中选择 "设计" 将页面切换到设计模式 () 并将其中一个控件拖动到标记为 " WebPartZone2" 的空区域中,则会以不同方式呈现控件的内容,这是因为自定义 WebPartChrome 对象不用于呈现在标准区域中。If you switch the page into design mode (by selecting Design in the Display Mode drop-down list control) and drag one of the controls into the empty zone labeled WebPartZone2, the contents of the control are rendered differently, because the custom WebPartChrome object is not being used for the rendering in a standard zone. 这与你尝试将自定义 WebPartChrome 类与类除类之外的任何其他区域一起使用时的效果相同 MyZone ,原因是前面的源代码。This is the same effect that you would achieve if you tried to use the custom WebPartChrome class with any other zone besides the MyZone class, due to the preceding source code.


RenderPartContents方法使你可以重写的正文区呈现 webPart ,同时将页眉和页脚呈现为默认呈现。The RenderPartContents method enables you to override the rendering of the body area of webPart, while leaving rendering of the header and footer to the default rendering.


您可以选择重写 RenderPartContents(HtmlTextWriter, WebPart) 方法。You can optionally override the RenderPartContents(HtmlTextWriter, WebPart) method. 如果是这样,只需执行所需的任何呈现自定义 webPart ,然后调用其 RenderControl(HtmlTextWriter) 方法即可。If so, you can simply perform whatever rendering customizations you want for webPart, and then call its RenderControl(HtmlTextWriter) method. 如果还想要在出现连接错误时依赖于默认呈现,请先调用基方法,然后自定义 writer 从基方法返回的。If you also want to rely on the default rendering in the event of connection errors, call the base method first, and then customize the writer that is returned from the base method.