컨트롤의 Visible 속성 가져 오기는 컨트롤을 렌더링할지 여부를 나타 내기 위해 반복적으로 트리를 찾습니다.Asp.Net에서 Visible 속성의 설정/실제 값을 얻는 방법
컨트롤의 "로컬"가시 값이 부모 컨트롤의 설정에 관계없이 무엇인지 확인하는 방법이 필요합니다. 즉, 그 자체가 참 또는 거짓으로 설정되었는지의 여부.
이 질문은 로컬 상태를 얻기 위해 Reflection을 사용하는 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
GetState의 GetMethod는 System.Web.UI.Control에 대해 null을 반환합니다. –