控件的Visible屬性的Get of遞歸查找樹以指示是否將顯示控件。如何在Asp.Net中獲取Visible屬性的集合/實際值
我需要一種方法來查看控件的「本地」可見值是什麼,而不管其父控件設置爲什麼。即它本身是否設置爲真或假。
我看到這個問題,How to get the 「real」 value of the Visible property?它使用反射來獲得本地狀態,但是,我一直無法得到這個WebControls的工作。這也是一個相當髒的獲取價值的方法。
我想出了以下擴展方法。它的工作方式是從其父項中移除控件,檢查屬性,然後將控件放回到找到它的位置。
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;
}
這是一個可以接受的方式嗎?它工作正常,我沒有遇到任何性能問題。它看起來非常骯髒,我毫不懷疑可能會出現它可能會失敗的實例(例如,實際渲染時)。
如果有人對這個解決方案有什麼想法,或者還有更好的找到價值的方法,那就太好了。
當您使用反射方法時遇到什麼問題? – slugster 2010-01-05 10:28:56
GetMethod for GetState爲System.Web.UI.Control返回null。 – 2010-01-05 10:37:13