ASP ContentPlaceHolder コントロールは「名前付けコンテナー」です (INamingContainer インターフェイスを実装します)。Control.FindControls メソッドは、現在の名前付けコンテナー内で、指定した ID を持つコントロールのみを検索します。
「/」で区切られた文字列を受け入れて、ページ上のネーミング コンテナーを任意にナビゲートするユーティリティ関数をときどき含めました。次の実装のようなもの。(注: このコードのコンパイルやテストは行っていません)
public static Control FindControlByPath(this Control start, string controlPath)
{
if(controlPath == null)
throw new ArgumentNullException("controlPath");
string[] controlIds = controlPath.split('/');
Control current = start;
if(controlIds[0] == "") // in case the control path starts with "/"
current = start.Page; // in that case, start at the top
for(int i=0; i<controlIds.Length; i++)
{
switch(controlIds[i])
{
case "":
// TODO: handle syntax such as "<controlId>//<controlId>", if desired
break;
case ".":
// do nothing, stay on the current control
break;
case "..":
// navigate up to the next naming container
current = current.Parent;
if(current == null)
throw new ArgumentOutOfRangeException("No parent naming container exists.", "controlPath");
while(!(current is INamingContainer))
{
current = current.Parent;
if(current == null)
throw new ArgumentOutOfRangeException("No parent naming container exists.", "controlPath");
}
break;
default:
current = current.FindControl(controlIds[i]);
break;
}
}
return current;
}
したがって、あなたの場合、次のことができるはずです。
<some control>.FindControlByPath("/MainLinks/litNavLinks").Text = sb.ToString();
また
Page.FindControlByPath("MainLinks/litNavLinks").Text = sb.ToString();