所有する子コントロール群のEnable/Disableをまとめて変更する

ASP.NETで、WebフォームであるPanelを利用した時は、PanelのEnabled=falseでPanelが保有するコントロールも使用不可になる。

だが、Tableをサーバサイドで利用するようにして、Disabled=Trueにしても子コントロールは使用不可表示になるが使用できてしまう。

このロジックはその問題を回避する。



001 Public Shared Sub SetControlEnabled(ByVal Parent As Control, ByVal IsEnabled As Boolean)
002 Dim objControl As Control
003
004 SetEnabled(Parent, IsEnabled)
005
006 For Each objControl In Parent.Controls
007 If objControl.Controls.Count > 0 Then
008 SetControlEnabled(objControl, IsEnabled)
009 Else
010 SetEnabled(objControl, IsEnabled)
011 End If
012 Next
013 End Sub
014
015 Private Shared Sub SetEnabled(ByVal objControl As Control, ByVal IsEnabled As Boolean)
016 Dim objProperty As System.Reflection.PropertyInfo
017 objProperty = objControl.GetType().GetProperty("Disabled")
018 If Not objProperty Is Nothing Then
019 objProperty.SetValue(objControl, (Not IsEnabled), Nothing)
020 Else
021 objProperty = objControl.GetType().GetProperty("Enabled")
022 If Not objProperty Is Nothing Then
023 objProperty.SetValue(objControl, (IsEnabled), Nothing)
024 End If
025 End If
026
027 End Sub
028