Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NEt WebForms UserControl: Why is overriding .Visible causing a System.StackOverflowException?
    primarykey
    data
    text
    <p>I have a custom UserControl whose .Visible property I need to set to True or False. However, if the UserControl's first child control is a Panel and its ID is cMain, I want it to set the Panel's .Visible property instead of the UserControl's. Here's my code:</p> <p>Here's the custom class I use:</p> <pre><code>Public MustInherit Class MyControl : Inherits UserControl Public Overrides Property Visible As Boolean Get Return GetVisible() End Get Set(value As Boolean) SetVisible(value) End Set End Property Function GetVisible() As Boolean Dim c As Control = GetMain() If TypeOf c Is Panel Then Return c.Visible Else Return MyBase.Visible End If End Function Sub SetVisible(Value As Boolean) Dim c As Control = GetMain() If TypeOf c Is Panel Then c.Visible = Value Else MyBase.Visible = Value End If End Sub Function GetMain() As Control Dim c As Control = If(Controls.Count = 0, Nothing, Controls(0)) If Not (TypeOf c Is Panel AndAlso c.ID = "cMain") Then c = Me Return c End Function End Class </code></pre> <p>Here's the actual UserControl itself:</p> <pre><code>&lt;%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="JsonJqueryDevex.TestControl1" %&gt; &lt;asp:Panel ID="cMain" runat="server"&gt; inside &lt;/asp:Panel&gt; outside </code></pre> <p>UserControl's codebehind:</p> <pre><code>Public Class TestControl1 Inherits MyControl Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub End Class </code></pre> <p>Here's the markup to implement it into the host page:</p> <pre><code>&lt;uc:TestControl ID="ucTest" Visible="true" runat="server"&gt;&lt;/uc:TestControl&gt; </code></pre> <p>Notice that I overrode .Visible within my base clase. I did this so that I could call MyBase if the control being referenced is the UserControl itself. Otherwise, I assume it's the Panel control. When I load the page, I get <code>System.StackOverflowException</code>. What's funny is that I don't get this when I have the custom control's Visible property set to <code>false</code> in the markup.</p> <p>The stack trace shows that it's getting caught in .Visible's get accessor when it calls <code>Return GetVisible()</code>. If it's a Panel, it will execute <code>Return c.Visible</code>. However, as soon as I reference .Visible when c is a Panel, it breaks back into MyControl's .Visible get accessor. I don't know how this is possible since I am only overriding my custom control's Visible property, but it's acting as if I am overriding the Panel's .Visible property. What is going on here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload