How can I get the parent page from a User Control in an ASP.NET Website (not Web Application)
Asked Answered
W

9

34

Just as the subject asks.

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?

Weirick answered 22/6, 2009 at 19:19 Comment(1)
There's no need to store the reference- each UserControl already exposes a Page property.Denton
D
64
this.Page

or from just about anywhere:

Page page = HttpContext.Current.Handler as Page
Denton answered 22/6, 2009 at 19:21 Comment(0)
D
14

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.

Desperate answered 22/6, 2009 at 19:23 Comment(4)
this is a great point. well defined user controls should be completely independent of the page they're on.Pharisee
If the application in question is a silo'ed application and all pages inherit from a BasePage class and you need to access that BasePage then this.Page is a completely acceptable solution as all pages in that application will inherit from that BasePage and the Control will be defined solely for that application.Psychosurgery
A good use case for this is to access the Page.Items collection, which can be used to store objects that are used for the lifetime of the Asp.net page processing cycle. You can avoid reloading common objects in every user control.Nemesis
That is great in an ideal world, but there are many cases where parts of a control may have a condition requirement dependent on the calling page. A good example is if a control is used on every page and that control displays or logs it's calling page (for whatever reason, like internal tracking, debugging, even security reasons).Outplay
I
10

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");
Implausible answered 22/6, 2009 at 19:22 Comment(0)
B
2

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);
}
Bioclimatology answered 22/6, 2009 at 19:28 Comment(0)
W
2

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.

Weirick answered 22/6, 2009 at 19:31 Comment(0)
M
1

You must use NamingContainer like that:

       try
        {
            if (!string.IsNullOrWhiteSpace(TargetCtrlID))
            {
                var ctrl = NamingContainer.FindControl(TargetCtrlID);
                if(ctrl != null)
                    Console.Write("'" + ctrl.ClientID + "'");
            }
        }
        catch
        {

        }
Microorganism answered 9/4, 2012 at 16:38 Comment(0)
C
0

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

Chuckhole answered 22/6, 2009 at 19:27 Comment(1)
Except if the uc is in another uc on a tab on a page..... etc. etc. ??Sarracenia
A
0

Create a delegate in the user control and then assign it a method from the parent page.

class MyUserControl : UserControl
{
   delegate object MyDelegate(object arg1, object arg2, object argN);
   public MyDelegate MyPageMethod;

   public void InvokeDelegate(object arg1, object arg2, object argN)
   {
     if(MyDelegate != null)
        MyDelegate(arg1, arg2, argN); //Or you can leave it without the check 
                                      //so it can throw an exception at you.
   }
}

class MyPageUsingControl : Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
     if(!Page.IsPostBack)
        MyUserContorlInstance.MyPageMethod = PageMethod;
   }

   public object PageMethod(object arg1, object arg2, object argN)
   {
     //The actions I want
   }
}
Ackerman answered 13/12, 2013 at 10:50 Comment(0)
O
0

Writing to Page and Master Page from Web User Control: Personally I like the user controls to be loose, but it can be done like this.

Master Page:

public partial class Second : System.Web.UI.MasterPage
    {
        public void SecondMasterString(string text)
        {
            MasterOut.Text += text;
        }
    }

Directive needed on WebForm1 : So page can write to master page

<%@  MasterType VirtualPath="~/DemoFolder/MasterPages/Second.master" %>

Methods write to page and Master Page:

public partial class WebForm1 : System.Web.UI.Page
    {
    public void SetPageOutput(string text)
        {
            // writes to page
            this.PageOut.Text = text;
        }

        public void SetMaster(string text)
        {
            // writes to Master Page
            this.Master.SecondMasterString(text);
        }
}

User Control writes to both Page and Master Page:

public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LegoLand.DemoFolder.MasterPages.WebForm1 page = (WebForm1)this.Parent.Page;
            page.SetMaster("** From the control to the master");
            page.SetPageOutput("From the control to the page");
        }
    }
Osteopath answered 29/1, 2019 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.