Steele Priceのブログ投稿からこの関数を見つけたところ、完全に機能しました。マスターページのあるページ内でユーザーコントロールを参照しようとしましたが、これ以外に何も試しませんでした。コアクラスの 1 つに入れます。詳細については、 Steele のブログ投稿を参照してください。
これをクラスに入れる場合、次のようなコントロール参照を取得する必要があります。
Dim imgStep2PreviewIcon As Image = Eyespike.Utilities.FindControl(Of Control)(Page, "imgStep1PreviewIcon")
imgStep2PreviewIcon.Visible = False
VB.NET コード
Public Shadows Function FindControl(ByVal id As String) As Control
Return FindControl(Of Control)(Page, id)
End Function
Public Shared Shadows Function FindControl(Of T As Control)(ByVal startingControl As Control, ByVal id As String) As T
Dim found As Control = startingControl
If (String.IsNullOrEmpty(id) OrElse (found Is Nothing)) Then Return CType(Nothing, T)
If String.Compare(id, found.ID) = 0 Then Return found
For Each ctl As Control In startingControl.Controls
found = FindControl(Of Control)(ctl, id)
If (found IsNot Nothing) Then Return found
Next
Return CType(Nothing, T)
End Function
C# (未テスト、converter.telerik.comを使用して生成)
public new Control FindControl(string id)
{
return FindControl<Control>(Page, id);
}
public static new T FindControl<T>(Control startingControl, string id) where T : Control
{
Control found = startingControl;
if ((string.IsNullOrEmpty(id) || (found == null))) return (T)null;
if (string.Compare(id, found.ID) == 0) return found;
foreach (Control ctl in startingControl.Controls) {
found = FindControl<Control>(ctl, id);
if ((found != null)) return found;
}
return (T)null;
}