WebPartChrome.RenderPartContents Method (HtmlTextWriter, WebPart)


Renders the main content area of a WebPart control, excluding the header and footer.

Namespace:   System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

protected virtual void RenderPartContents(
	HtmlTextWriter writer,
	WebPart webPart


Type: System.Web.UI.HtmlTextWriter

The HtmlTextWriter that receives the webPart content.

Type: System.Web.UI.WebControls.WebParts.WebPart

The control currently being rendered.

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.

Notes to Inheritors:

You can optionally override the RenderPartContents method. If so, you can simply perform whatever rendering customizations you want for webPart, and then call its RenderControl method. 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.

The following code example demonstrates use of the RenderPartContents method. For the full code required to run the example, see the Example section of the WebPartChrome class overview topic.

The following section from the code example demonstrates how to override the RenderPartContents method. The 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. Second, if the control is not selected, and if the control's zone is of type MyZone, the method renders the control. 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))

If you load the Web page in a browser, you can see that the content of each control is rendered normally. 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. 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.

.NET Framework
Available since 2.0
Return to top