Friday, September 5, 2008

Render vs RenderWebpart vs RenderContents Method

Scenario:
You want to write a basic webpart and want to render some HTML.

Option:
You can either override the Render , RenderWebPart or RenderContents methods to add your own HTML

Recommended:
Use the RenderContents() method.

Reason:
The WebPart base class seals the Render() method of System.Web.UI.Control because the Web Part infrastructure needs to control rendering the contents of a Web Part. For this reason, custom Web Parts must override the RenderWebPart() method of the WebPart base class. But for making your webpart really future proof, suggested way is RenderContents() as future version will be using the same function to render the output.

Sample:

protected override void RenderContents(HtmlTextWriter output)
{
output.Write("Hello World");
}


Article:
http://msdn.microsoft.com/en-us/library/ms948909.aspx

0 comments: