ASP.NEt WebForms UserControl:为什么覆盖.Visible导致System.StackOverflowException?

时间:2012-09-18 16:38:05

标签: asp.net user-controls webforms override stack-overflow

我有一个自定义UserControl,其.Visible属性我需要设置为True或False。但是,如果UserControl的第一个子控件是Panel并且其ID是cMain,我希望它设置Panel的.Visible属性而不是UserControl。这是我的代码:

这是我使用的自定义类:

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

这是实际的UserControl本身:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="JsonJqueryDevex.TestControl1" %>
<asp:Panel ID="cMain" runat="server">
inside
</asp:Panel>
outside

UserControl的代码隐藏:

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

这是将其实现到主页中的标记:

<uc:TestControl ID="ucTest" Visible="true" runat="server"></uc:TestControl>

请注意,我在我的基础分段中覆盖了.Visible。我这样做是为了我可以调用MyBase,如果被引用的控件是UserControl本身。否则,我认为它是Panel控件。当我加载页面时,我得到System.StackOverflowException。有趣的是,当我在标记中将自定义控件的Visible属性设置为false时,我没有得到这个。

堆栈跟踪显示它在调用Return GetVisible()时被捕获.Visible的get访问器。如果是Panel,它将执行Return c.Visible。但是,一旦我引用.Visible,当c是一个Panel时,它就会重新进入MyControl的.Visible get访问器。我不知道这是怎么可能的,因为我只是覆盖了我的自定义控件的Visible属性,但它表现得好像我重写了Panel的.Visible属性。这是怎么回事?

0 个答案:

没有答案