In the code-behind Page
is a property of type Page
(confusing I know!) so it refers to an instance of the Page
class. To your static method outside of this scope it only know of the class Page
and so the compiler sees you attempting to call a static property called Controls
which doesn't exist on the Page
class and so throws an error your way.
You will need to pass the Page
instance to the MessageBox
method:
public static void MessageBox(Page page, string msg)
{
Label lbl = new Label();
lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
page.Controls.Add(lbl);
}