How can I get the parent page from a User Control

2019-03-17 06:15发布

问题:

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?

回答1:

this.Page

or from just about anywhere:

Page page = HttpContext.Current.Handler as Page


回答2:

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.



回答3:

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");


回答4:

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);
}


回答5:

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.



回答6:

You must use NamingContainer like that:

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

        }


回答7:

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



回答8:

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
   }
}


回答9:

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");
        }
    }