1

I am trying to find out whether a particular control on an asp.net page has had it's "Visible" property assign to true or false. The problem is that the visible property crawl up the list of parents and if any of them show as invisible, the queried control will also show as invisible. I need to know what the control itself has been set to.

I did some searching and found the post How to get the set/real value of the Visible property in Asp.Net which offered the following solution

public static bool LocalVisible(this Control control){
    var flags = typeof (Control)
        .GetField("flags", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(control);

    return ! (bool) flags.GetType()
        .GetProperty("Item", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(flags, new object[] {0x10});
}

But when I tried it, it returned an "Ambiguous Match Found" error on GetProperty.

Can someone point out what I'm doing wrong, or show another way of getting what I want?

Community
  • 1
  • 1
Hypersapien
  • 617
  • 2
  • 8
  • 23

1 Answers1

1

I had the same problem (two years later). This is the answer that I just wrote in the topic that you refer to:

In case someone tries to get Jørn Schou-Rode's code working in VB.NET, here is the code that works for me. When I simply translate his code in VB, I get an "Ambiguous match found" exception, because there are 3 flavors of the flags "Item" property.

<Extension()>
Public Function GetLocalVisible(ctl As Control) As Boolean
    Dim flags As Object = GetType(Control).GetField("flags", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(ctl)
    Dim infos As PropertyInfo() = flags.GetType().GetProperties(BindingFlags.Instance Or BindingFlags.NonPublic)
    For Each info As PropertyInfo In infos
        If info.Name = "Item" AndAlso info.PropertyType.Name = "Boolean" Then
            Return Not CBool(info.GetValue(flags, New Object() {&H10}))
        End If
    Next
    Return ctl.Visible
End Function
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146