I have a Literal control that I am trying to locate so I can insert text into it. I have a Master page that contains several content placeholders.
The ASP ContentPlaceHolder control is a "naming container" (it implements the INamingContainer interface). The Control.FindControls method only searches within the current naming container for a control with the ID that you specify.
I've occassionally included a utility function that accepts a "/" delimited string to arbitrarily navigate through the naming containers on a page. Something like the following implementation. (Note: I have not tried to compile or test this 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//", 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;
}
So, in your case you should be able to do the following:
.FindControlByPath("/MainLinks/litNavLinks").Text = sb.ToString();
or
Page.FindControlByPath("MainLinks/litNavLinks").Text = sb.ToString();