Good afternoon! I've spent half my day trying to figure this out; hopefully one of you can help.
I have written a limited CMS and am working on allowing content from the CMS to be displayed as interstitial pages within external sites. To do this, I wanted to create a WCF service that would render the appropriate content and return it as a string to be placed into the client page.
However, I've run into a strange behavior. The RadioButtonList
and CheckBoxList
controls end up throwing a NullReferenceException
during RenderControlInternal
(obviously not my contribution). All other controls (textboxes, listboxes, dropdowns, custom composite controls) render fine.
I created an ASMX service alongside the WCF service, and cut/pasted the code directly from the WCF service. It renders the controls perfectly.
Any clues why the WCF service selectively causes bombs?
Here is the service method code:
public string RenderPageContents(int pageId)
{
string output = string.Empty;
SiteBuilder.Page p = new SiteBuilder.Page();
if (p.Load(pageId))
{
foreach (SiteBuilder.PageItem pi in p.Items)
{
if (pi.LoadItemDetails(true))
{
// Calls LoadControl from \Bin
Web.Controls.Base.BuilderControlBase bcb =
Web.Controls.ControlHelper.CreateSiteBuilderControl(
@"C:\Development\SitebuilderService\",
pi.ItemDetails);
if (bcb != null)
{
bcb.Configure(pi);
output += ControlHelper.RenderControl(ref bcb);
}
}
}
}
return output;
}
Here is the code for ControlHelper.RenderControl
(yes, it's in VB. It lives in another project).
Public Shared Function RenderControl(ByRef ctrl As Controls.Base.BuilderControlBase) _
As String
Dim sb As New StringBuilder()
Dim tw As New StringWriter(sb)
Dim hw As New HtmlTextWriter(tw)
ctrl.RenderControl(hw)
Return sb.ToString()
End Function
The exception message is "Object reference not set to an instance of an object."
Here is a stacktrace of the Exception thrown when WCF is used:
at System.Web.UI.WebControls.RadioButtonList.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at XXX.ControlHelper.RenderControl(BuilderControlBase& ctrl)
in C:\TFS\XXX\Development\XXX\Web\Controls\Builder\Helpers\ControlHelper.vb:line 81
at XXX.Services.SiteBuilderService.RenderPageContents(Int32 pageId)
in C:\TFS\Development\XXX.Web.Services\XXX.Web.Services\SiteBuilderService.svc.cs:line 61