I have a version 4.6 WebForms web site and a simple, custom WebControl. When I do something like:
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public SomeControl Contents { get; set; }
public override void RenderControl(HtmlTextWriter writer) {
// Simplified code
Contents.RenderControl(writer);
}
... I can insert the control exactly once i a page and all is fine. If however I insert the control more than once, or if I add another property like Contents
in the example above and then run RenderControl
on both controls in the local override (i.e. in the body of public override void RenderControl
), I get an exception:
[InvalidOperationException: Stack empty.] System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52 System.Collections.Generic.Stack`1.Pop() +6856361
Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject) +84
System.Web.UI.RenderTraceListenerList.EndRendering(TextWriter writer, Object renderedObject) +66
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +170
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +9796138
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1303
Apparently, this error is caused by the Visual Studio Browser Link feature (or its subsequent functions). If I disable it the error goes away.
This same problem has been discussed in other contexts like in this question, and here and also over here to point out but a few examples, and several fixes have been suggested. The fixes though are all related to completely or partially disable the Browser Link or Page Inspector features.
I would like to know if anyone has a solution to prevent the error from occuring by altering the code within the public override void RenderControl
method of my code example, introducing some checking of something or whatever?