Friday, February 11, 2005
« February .NET User Group Meeting Success... | Main | Eliminating Sql Log »

If you happen to ever create ASP.NET UserControls (e.g. .ASCX files) then this might be of interest.  Oftentimes, in an attempt to create useful, dynamic pages, developers will, via code, load control instances rather than rely solely on <tagPrefix:tagName /> declarations on the .ascx page itself.

Instead, we'll frequently call this.LoadControl(”...ascx”) to load the control and then add it to the .Controls collection of the parent .ascx.  This is all well and good and works remarkably well.  However, if you start to do anything fancy in the child .ascx files you may be greeted with the all-too-helpful HttpException: “External component has thrown an exception”.  This can be somewhat disconcerting because the error throws on the call to LoadControl and doesn't provide any feedback as to the cause of the error.

It's time to investigate the child .ascx's HTML.  More often than not, you have some malformed HTML that the server just cannot process.  A situation that I frequently encounter is that the developer created a server-side HTML table control that contains malformed child controls.  The following exemplifies this:

<%@ Control language=”c#” AutoEventWireup=”false” Inherits=”...” %>
<%@ Register tagPrefix=“custom“ tagName=“MyDataRow“ src=“~/.../childcontrol.ascx“ %>
<table id=t runat=server>
   <custom:MyDataRow id=row runat=server />
</table>

While the MyDataRow class might render the contents within <tr><td>...</td></tr> tags that results in good client code, the server code is malformed and the HTML table cannot initialize properly within the UserControl on the web server.

Word to the wise, when dealing with UserControls (especially those that rely on runat=server controls), always ensure that the HTML is well-formed.

Friday, February 11, 2005 7:47:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [4]  |  Trackback