You need a reference to the control or the page where this control is sitting in. This page must be running though an actual lifecycle. So for example from a webmethod you cannot access a control.
However, then you can access this control even from a static method, which seems to be what you want:
public static void SetControlText(string controlID, string text)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
Control ctrl = FindControlRecursive(page, controlID);
if(ctrl != null)
{
ITextControl txt = ctrl as ITextControl;
if(txt != null)
txt.Text = text;
}
}
}
public static 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;
}
Now this works from everywhere during the lifefycle of a page:
SetControlText("ltlContents", "Hello world");