Careful with your output

Creating SharePoint Web Parts and Controls can be tedious at best. Lots of compile, click, refresh, going on. Two tips I wanted to pass along to budding developers today.

Commented out controls still evaluate
Let's say you're writing a new Control and embedding it on an .aspx page like so:

<%@ Register Tagprefix="ABC" Namespace="..." >
<ABC:MyControl runat="server" />

Let's say we want to turn this control off so naturally you might do this:

<%@ Register Tagprefix="ABC" Namespace="..." >
<!-- <ABC:MyControl runat="server" /> -->

In fact, the control is still executed by ASP.NET (or the SharePoint ISAPI filter, I'm not sure which) and whatever output that control creates will go onto the page. Grant you, it's still wrapped in comments but if your control is throwing an exception, you might be scratching your head wondering why it's doing this. So to be safe, just comment out or rename your Render method your code (Note: if you don't inherit from SPControl you might not get any output, but then why are you not inheriting from SPControl?).

Web Parts that output nothing still output something
Another quirk is the base classes in SharePoint. All Web Parts are inherited from WebPart so even if your RenderWebPart or CreateChildControls method does nothing you'll still end up with output something like this on the page where a Web Part lives:

<table TOPLEVEL border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td valign="top">
<div WebPartID="00000000-0000-0000-0000-000000000000" HasPers="false" id="WebPartWPQ1" width="100%" class="ms-WPBody" allowDelete="false" style=""></div>
</td>
</tr>
</table>

So just watch that your WebParts will still render even if you're code doesn't output anything.

1 Comment

Comments have been disabled for this content.