如何获得在Asp.Net的Visible属性的设定/实际价值如何获得、属性、实际、价值

2023-09-03 03:57:34 作者:无言独上西楼

控件的Visible属性的get递归查找树表示,如果控制将呈现与否。

The Get of the Visible property of a control recursively looks up the tree to indicate if the control will be rendered or not.

我需要一种方法来看到一个控制的本地可见价值是什么,无论什么它的父控​​件设置。即无论是自身设置为true或false。

I need a way to see what the "local" visible value of a control is regardless of what its parent controls are set to. i.e. Whether it itself was set to true or false.

我已经看到了这个问题,How得到的Visible属性的真正的价值呢?它使用反射来获取本地状态,不过,我一直没能得到这个工作的器WebControls。这也是获得价值相当肮脏的方法。

I have seen this question, How to get the "real" value of the Visible property? which uses Reflection to obtain the local state, however, I have not been able to get this working for WebControls. It's also a rather dirty method of getting the value.

我想出了下面的扩展方法。它的工作原理通过从其父控制,检查的财产,然后把控制权交还给它找到了。

I have come up with the following extension method. It works by removing the control from its parent, checking the property, then putting the control back where it found it.

    public static bool LocalVisible(this Control control)
    {
        //Get a reference to the parent
        Control parent = control.Parent;
        //Find where in the parent the control is.
        int index = parent.Controls.IndexOf(control);
        //Remove the control from the parent.
        parent.Controls.Remove(control);
        //Store the visible state of the control now it has no parent.
        bool visible = control.Visible;
        //Add the control back where it was in the parent.
        parent.Controls.AddAt(index, control);
        //Return the stored visible value.
        return visible;
    }

这是这样的一种可接受的方式?它工作正常,我还没有遇到任何性能问题。这似乎只是非常脏,我毫不怀疑,有可能是在它可能会失败的实例(例如,当实际渲染)。

Is this an acceptable way of doing this? It works fine and I haven't come across any performance issues. It just seems extremely dirty and I have no doubt there could be instances in which it might fail (for example, when actually rendering).

如果任何人有这个解决方案有什么想法或者更好的是寻找价值,那么这将是巨大的更好的方式。

If anyone has any thoughts on this solution or better still a nicer way of finding the value then that would be great.

推荐答案

周围挖了一下后,我公司生产的使用反射来检索可见标志的类 System.Web.UI.Control :

After digging around a bit, I have produced the following extension method that uses reflection to retrieve the value of the Visible flag for classes inheriting from System.Web.UI.Control:

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});
}

它使用反射来找到控制对象中的私有字段标志。该字段与一个内部类中声明,所以更多的思考是需要调用它的索引属性的getter。

It uses reflection to find the private field flags within the Control object. This field is declared with an internal type, so more reflection is needed to invoke the getter of its indexer property.

扩展方法已经过测试,在下面的标记:

The extension method has been tested on the following markup:

<asp:Panel Visible="false" runat="server">
    <asp:Literal ID="litA" runat="server" />
    <asp:Literal ID="litB" Visible="true" runat="server" />
    <asp:Literal ID="litC" Visible="false" runat="server" />
</asp:Panel>

<asp:Literal ID="litD" runat="server" />
<asp:Literal ID="litE" Visible="true" runat="server" />
<asp:Literal ID="litF" Visible="false" runat="server" />

测试结果:

litA.LocalVisible() == true
litB.LocalVisible() == true
litC.LocalVisible() == false
litD.LocalVisible() == true
litE.LocalVisible() == true
litF.LocalVisible() == false