L'ASP ContentPlaceHolder control est un « conteneur nommant » (il implémente l'interface INamingContainer) . Le Control.FindControls method recherche uniquement dans le conteneur de dénomination actuel pour un contrôle avec l'ID que vous spécifiez.
J'ai parfois inclus une fonction d'utilité qui accepte une chaîne délimitée "/" pour naviguer arbitrairement à travers les conteneurs de dénomination sur une page. Quelque chose comme l'implémentation suivante. (Note: Je ne l'ai pas essayé de compiler ou tester ce code)
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;
}
Donc, dans votre cas, vous devriez être en mesure de faire ce qui suit:
<some control>.FindControlByPath("/MainLinks/litNavLinks").Text = sb.ToString();
ou
Page.FindControlByPath("MainLinks/litNavLinks").Text = sb.ToString();
N'avez pas essayé, mais pouvez-vous utiliser litNavLinks.Text = sb.ToString()? –
Voyant cela avec la réponse de Darin. Je vais devoir l'essayer le matin. Je n'ai pas essayé et je parie que ça marchera. Je me sens stupide si c'est le cas. Je me sens stupide en pensant que c'est juste. –
Parfois, ce sont les choses simples qui nous font voyager. –