編集:あなたの質問を読み直した後、投稿の2番目のリンクに基づいて突き刺しました:
public static T FindControl<T>(System.Web.UI.Control Control) where T : class
{
T found = default(T);
if (Control != null && Control.Parent != null)
{
if(Control.Parent is T)
found = Control.Parent;
else
found = FindControl<T>(Control.Parent);
}
return found;
}
テストされていないことに注意してください。これは今作成したばかりです。
以下、参考までに。
FindControlRecursive という共通関数があり、ページからコントロール ツリーをたどって特定の ID を持つコントロールを見つけることができます。
これはhttp://dotnetslackers.com/Community/forums/find-control-recursive/p/2708/29464.aspxからの実装です。
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
これは次のように使用できます。
var control = FindControlRecursive(MyPanel.Page,"controlId");
これをhttp://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspxと組み合わせて、より優れたバージョンを作成することもできます。