How to access the owner Page from within a component

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default How to access the owner Page from within a component

    I have a component (System.ComponentModel.Component)
    and want to access the Page that "owns" it, during runtime.

    Yes, I can write a public property and set that property in the
    designer, and thereby get access to it at runtime, but I want to be
    smarter than that.

    Somehow the component has to "know" the Page it sits on.
    I want access to that Page object early in the initialization process,
    when the component is created.
    If I have that Page object, I can for example add certain events to the
    Page.

    Any ideas?

    --
    Ingvar Nilsen


    Ingvar Nilsen Guest

  2. Similar Questions and Discussions

    1. You are about to access a Activex component
      Hi, We are trying to access a .net dll based method from classic asp code. When the method is accessed, system gives a message "You are about to...
    2. VB Component debugging as anonymous access
      Hello, I'd like to debug a VB component without activating Basic/Integrated security on the Web Server, just the anonymous access. I need this to...
    3. Can't access Active-X component within ASP page
      I am quite new to the ASP scene. I want to display the results of a recordset into a third party grid control called VSFlexgrid. I've prepared one...
    4. Cannot access the registered component, please help!
      I am using Win2003 + IIS, it cannot access a registered component from my bowser and prompts the following error: Server object error 'ASP 0178 :...
    5. DB Access Component
      Hi, Can i have the Connection object being passed as the parameter from the Component(Dll) i am using to connect to the databaese to the ASP page....
  3. #2

    Default Re: How to access the owner Page from within a component

    "Ingvar Nilsen" <telcontr@online.no> wrote in message
    news:Ogvtv195EHA.208@TK2MSFTNGP12.phx.gbl...
    >I have a component (System.ComponentModel.Component)
    > and want to access the Page that "owns" it, during runtime.
    >
    > Yes, I can write a public property and set that property in the
    > designer, and thereby get access to it at runtime, but I want to be
    > smarter than that.
    >
    > Somehow the component has to "know" the Page it sits on.
    > I want access to that Page object early in the initialization process,
    > when the component is created.
    > If I have that Page object, I can for example add certain events to the
    > Page.
    Having the component add events to the page is backwards. The component
    should expose events which the Page can listen to if it wants to. What
    exactly do you mean, "add events to the page"?

    John Saunders


    John Saunders Guest

  4. #3

    Default Re: How to access the owner Page from within a component

    John Saunders wrote:
    > Having the component add events to the page is backwards.
    Perhaps, but before we discuss that, how do I get access to the page?
    I am sure there is a way, but how?

    > The component should expose events which the Page can listen to
    > if it wants to.
    Yes, that is the normal case. And it also does.
    > What exactly do you mean, "add events to the page"?
    Mostly to add an event handler for Page_Load and for Page_Unload.
    I do this already, when the published property "Page" I use today is
    initialized, because this happens before Page_Load. And it works great.

    What I wanted to do is to avoid having to explicit set the Page
    property at design time. The Page is there, at runtime, it has to be,
    it "owns" the component, I just can't get hold of it because I do not
    know how to, as simple as that.

    BTW, this question comes up now and then (Google), no proper answer
    found so far, strangely enough.

    --
    Ingvar Nilsen


    Ingvar Nilsen Guest

  5. #4

    Default Re: How to access the owner Page from within a component

    "Ingvar Nilsen" <telcontr@online.no> wrote in message
    news:uzOAOpD6EHA.2540@TK2MSFTNGP09.phx.gbl...
    > John Saunders wrote:
    >> Having the component add events to the page is backwards.
    >
    > Perhaps, but before we discuss that, how do I get access to the page?
    > I am sure there is a way, but how?
    There is no way in general.
    >> The component should expose events which the Page can listen to
    >> if it wants to.
    >
    > Yes, that is the normal case. And it also does.
    >
    >> What exactly do you mean, "add events to the page"?
    >
    > Mostly to add an event handler for Page_Load and for Page_Unload.
    > I do this already, when the published property "Page" I use today is
    > initialized, because this happens before Page_Load. And it works great.
    Ok, it sounded like you wanted to add event handlers _to_ the page, which I
    couldn't see how you could do.
    > What I wanted to do is to avoid having to explicit set the Page
    > property at design time. The Page is there, at runtime, it has to be,
    > it "owns" the component, I just can't get hold of it because I do not
    > know how to, as simple as that.
    If you take a look at InitializeComponent in your code behind you'll see
    that the page does not "own" the component. If the component has a
    "component constructor", then the "components" variable on the page contains
    the components, but there's no link from "components" to the page.

    John Saunders


    John Saunders Guest

  6. #5

    Default Re: How to access the owner Page from within a component

    John Saunders wrote:
    > If you take a look at InitializeComponent in your code behind you'll
    > see that the page does not "own" the component.
    Fair enough, the expression "own" is more figuratively than technically
    correct. But what is "this" in this context, if not the page?

    this.components = new System.ComponentModel.Container();
    this.MyComponent1 = new MyComponent(this.components);

    I still believe there must be a way.
    When I can get access to the Response object ar runtime, I should get
    access to the page too.
    > If the component has
    > a "component constructor", then the "components" variable on the page
    > contains the components, but there's no link from "components" to the
    > page.
    I am not familiar with this architecture.
    I am used to components always knowing who contains/created them, and
    the creator in turn about its creator again and so on.
    By using the "Parent" or "Owner" property which here seems to not exist.

    --
    Ingvar Nilsen
    Ingvar Nilsen Guest

  7. #6

    Default Re: How to access the owner Page from within a component


    "Ingvar Nilsen" <telcontr@online.no> wrote in message
    news:uuyo8UF6EHA.4040@TK2MSFTNGP14.phx.gbl...
    > John Saunders wrote:
    >
    >> If you take a look at InitializeComponent in your code behind you'll
    >> see that the page does not "own" the component.
    >
    > Fair enough, the expression "own" is more figuratively than technically
    > correct. But what is "this" in this context, if not the page?
    >
    > this.components = new System.ComponentModel.Container();
    > this.MyComponent1 = new MyComponent(this.components);
    Yes, "this" is the page. But notice that it's not "this" that's being passed
    to the component, but "this.components".
    > I still believe there must be a way.
    > When I can get access to the Response object ar runtime, I should get
    > access to the page too.
    You can get access to the response object because the HttpContext class
    thoughtfully provides a static Current property. There is no Page.Current.

    Ahah! I just realized that your component should be able to find "the
    running page" assuming that there is one. Try HttpContext.Current.Handler.
    If HttpContext.Current is non-null, and .Handler is non-null, and if it is a
    page, then it's the currently-running page.

    Said page may not be available in the constructor of your component, BTW.

    John Saunders
    >> If the component has
    >> a "component constructor", then the "components" variable on the page
    >> contains the components, but there's no link from "components" to the
    >> page.
    >
    > I am not familiar with this architecture.
    > I am used to components always knowing who contains/created them, and
    > the creator in turn about its creator again and so on.
    > By using the "Parent" or "Owner" property which here seems to not exist.
    >
    > --
    > Ingvar Nilsen

    John Saunders Guest

  8. #7

    Default Re: How to access the owner Page from within a component

    "Ingvar Nilsen" <telcontr@online.no> wrote in message
    news:uuyo8UF6EHA.4040@TK2MSFTNGP14.phx.gbl...
    > John Saunders wrote:
    >
    >> If you take a look at InitializeComponent in your code behind you'll
    >> see that the page does not "own" the component.
    >
    > Fair enough, the expression "own" is more figuratively than technically
    > correct. But what is "this" in this context, if not the page?
    Well, I've got your solution right here!

    Or, more accurately, Chris Sells does. I've adapted his code from "Windows
    Forms Progeaming in C#", ISBN 0-321-11620-8, Chapter 9, "Design-Time
    Integration", page 314-315. The basic trick is to define a public property.
    The property getter runs at design time, where it can get a reference to the
    page through the designer infrastructure. This puts a reference to the page
    into this property at design time.

    When the designer serialzes the component to the code, it will emit code to
    store "this" into the property, so you'll have it at run time:

    private IComponent _page;
    [Browsable(false)]
    public IComponent ParentPage
    {
    get
    {
    if (_page == null && this.DesignMode)
    {
    System.ComponentModel.Design.IDesignerHost designer =
    this.GetService(typeof(System.ComponentModel.Desig n.IDesignerHost))
    as System.ComponentModel.Design.IDesignerHost;
    if (designer != null)
    {
    _page = designer.RootComponent;
    }
    }
    return _page;
    }
    set
    {
    if (!this.DesignMode)
    {
    if ((_page != null) && (_page != value))
    {
    throw new InvalidOperationException("Can't change
    ParentPage at run time");
    }
    }
    else
    {
    _page = value;
    }
    }
    }

    With this in place, the designer serializes the component as follows:

    this.myComponent2 = new MyComponent(this.components);
    //
    // myComponent2
    //
    this.myComponent2.ParentPage = this;


    Success!

    John Saunders






    John Saunders Guest

  9. #8

    Default Re: How to access the owner Page from within a component

    John Saunders wrote:
    > Yes, "this" is the page. But notice that it's not "this" that's being
    > passed to the component, but "this.components".
    Yes, I have. It takes time to getting used to. I find it strange that
    the component constructor doesn't have an argument for the instance
    creating it. And C# doesn't have virtual constructors, AFAIK, hm...

    > > I still believe there must be a way.
    > > When I can get access to the Response object ar runtime, I should
    > > get access to the page too.
    >
    > You can get access to the response object because the HttpContext
    > class thoughtfully provides a static Current property. There is no
    > Page.Current.
    Yes, have seen that.
    > Ahah! I just realized that your component should be able to find "the
    > running page" assuming that there is one. Try
    > HttpContext.Current.Handler.
    In hindsight, I really had plans to look at it but currently my help
    system is wobbly for some reason, so I skipped this property.
    > If HttpContext.Current is non-null
    It is not!
    > .Handler is non-null, and if it is a page, then it's the
    > currently-running page.
    ABSOLUTELY!
    > Said page may not be available in the constructor of your component,
    > BTW.
    Oh yeah, indeed it is!!!!

    Thanks a lot, John, with combined efforts we once again proved that in
    computing everything is possible if you look and try hard enough!.

    Now it works like magic, I can just drop a component on the web form,
    and this component itself installs event handlers for the Page's
    events, without writing a single line of code in the Page's code behind
    file.

    Exactly what I was after.

    --
    Ingvar Nilsen
    Ingvar Nilsen Guest

  10. #9

    Default Re: How to access the owner Page from within a component

    John Saunders wrote:
    > Well, I've got your solution right here!
    John, thanks a lot, I really appreciate your efforts!
    I have saved this solution, too, it may come in handy, but as you see
    form my other post, I achieved it with a couple of lines because the
    Current.Handler actually is precisely what I am after: The Page.

    Thanks again,

    --
    Ingvar Nilsen
    Ingvar Nilsen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139