34
this.Page
or from just about anywhere:
Page page = HttpContext.Current.Handler as Page
I cannot think of any good reason for a user control to know anything about the page it is on as the user control should be ignorant of its context and behave predictably regardless of what page it is on.
That being said, you can use this.Page.
I always used this.Page in the System.Web.UI.UserControl.
Or you can always do a recursive call on the Parent until u encounter an object that is a Page.
kind of overkill though...
protected Page GetParentPage( Control control )
{
if (this.Parent is Page)
return (Page)this.Parent;
return GetParentPage(this.Parent);
}
You must use NamingContainer like that:
try
{
if (!string.IsNullOrWhiteSpace(TargetCtrlID))
{
var ctrl = NamingContainer.FindControl(TargetCtrlID);
if(ctrl != null)
Console.Write("'" + ctrl.ClientID + "'");
}
}
catch
{
}