views:

1223

answers:

6

Just as the subject asks. Thanks!

EDIT 1

Maybe it's possible sometime while the request is being processed to store a reference to the parent page in the user control?

+3  A: 
this.Page

or from just about anywhere:

Page page = HttpContext.Current.Handler as Page
Paul Alexander
A: 

you can use the Parent property

if you need this to find a control on the page then you can use

Label lbl_Test = (Label)Parent.FindControl("lbl_Test");
jmein
A: 

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.

Andrew Hare
A: 

Every control has a parent property that you can use to access the parent.

protected void Page_Load(object sender, EventArgs e)
{
  Response.Write(this.Parent.ID);
}

EDIT: depends on which one of the lifecycle events of the page you want to store the reference and what use to store that reference. However the reference is always available

Marco Mangia
A: 

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);
}
BigBlondeViking
+1  A: 

Guess I didn't ask this correctly. I wanted to create a public method on the Page, and then call it from the User Page.

I found the way to do this is to create an interface, implement that interface, use this.Page to get the page from the control, cast it to the interface, then call the method.

Thanks for the timely responses everyone.

Joe Behymer