Export (0) Print
Expand All

WebPartChrome.RenderPartContents Method

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
)

Parameters

writer
Type: System.Web.UI.HtmlTextWriter

The HtmlTextWriter that receives the webPart content.

webPart
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>");
    else 
      if(this.Zone.GetType() == typeof(MyZone))
        part.RenderControl(writer);
}

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft