Why is this.Page NOT initialized when a composite control's CreateChildControls is being executed

Discussion in 'ASP .Net Building Controls' started by ~~~ .NET Ed ~~~, Apr 1, 2005.

  1. That is exactly what I have noticed, my web custom (composite) control gets
    instantiated and initialized on the page, so I see that
    CreateChildControls() is called as I am debugging it. What I find strange is
    that I see that while within CreateChildControls() the "this.Page" property
    is undefined!

    That gives me a few problems, namely that I cannot determine if I am in a
    PostBack or not, if I use this.Page.IsPostBack I get an "object reference
    not initialized". Or is there a way for a composite control to determine if
    it is being posted back? Anyway, concerning this particular item, I don't
    see the logic of having Page undefined, after all the parent page object
    exists and is actually creating its child controls (including the composite
    control). You can't have the chic without the chicken, or am I missing
    something out of this world?

    Emil
     
    ~~~ .NET Ed ~~~, Apr 1, 2005
    #1
    1. Advertising

  2. ~~~ .NET Ed ~~~

    Teemu Keiski Guest

    Hi,

    Can you determine at which exact stage your CreateChildControls gets called?
    (Tracing can help) Page property is set to a Control at Init stage of the
    lifecycle, and if CreateChildControls runs before that, answer is the null
    reference as you see.

    One design reason being here is that child controls should get added in
    CreateChildControls despite it being a postback or not, and the needs what
    to accomplish is it a postback or not, is done elsewhere/with different
    means. For example with databound controls CreateChildControls does what is
    needed when control renders itself from a ViewState without a datasource
    (actual control creation is done in DataBind method, when controls are
    created from datasource and certain view state data is set).

    Can you tell what youi are trying to do with the Page from the control?
    Perhaps registering a script block?

    --
    Teemu Keiski
    ASP.NET MVP, Finland

    "~~~ .NET Ed ~~~" <> wrote in message
    news:...
    > That is exactly what I have noticed, my web custom (composite) control
    > gets instantiated and initialized on the page, so I see that
    > CreateChildControls() is called as I am debugging it. What I find strange
    > is that I see that while within CreateChildControls() the "this.Page"
    > property is undefined!
    >
    > That gives me a few problems, namely that I cannot determine if I am in a
    > PostBack or not, if I use this.Page.IsPostBack I get an "object reference
    > not initialized". Or is there a way for a composite control to determine
    > if it is being posted back? Anyway, concerning this particular item, I
    > don't see the logic of having Page undefined, after all the parent page
    > object exists and is actually creating its child controls (including the
    > composite control). You can't have the chic without the chicken, or am I
    > missing something out of this world?
    >
    > Emil
    >
     
    Teemu Keiski, Apr 4, 2005
    #2
    1. Advertising

  3. I was simply trying to get the Context.

    "Teemu Keiski" <> wrote in message
    news:...
    > Hi,
    >
    > Can you determine at which exact stage your CreateChildControls gets
    > called? (Tracing can help) Page property is set to a Control at Init stage
    > of the lifecycle, and if CreateChildControls runs before that, answer is
    > the null reference as you see.
    >
    > One design reason being here is that child controls should get added in
    > CreateChildControls despite it being a postback or not, and the needs what
    > to accomplish is it a postback or not, is done elsewhere/with different
    > means. For example with databound controls CreateChildControls does what
    > is needed when control renders itself from a ViewState without a
    > datasource (actual control creation is done in DataBind method, when
    > controls are created from datasource and certain view state data is set).
    >
    > Can you tell what youi are trying to do with the Page from the control?
    > Perhaps registering a script block?
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, Finland
    >
    > "~~~ .NET Ed ~~~" <> wrote in message
    > news:...
    >> That is exactly what I have noticed, my web custom (composite) control
    >> gets instantiated and initialized on the page, so I see that
    >> CreateChildControls() is called as I am debugging it. What I find strange
    >> is that I see that while within CreateChildControls() the "this.Page"
    >> property is undefined!
    >>
    >> That gives me a few problems, namely that I cannot determine if I am in a
    >> PostBack or not, if I use this.Page.IsPostBack I get an "object reference
    >> not initialized". Or is there a way for a composite control to determine
    >> if it is being posted back? Anyway, concerning this particular item, I
    >> don't see the logic of having Page undefined, after all the parent page
    >> object exists and is actually creating its child controls (including the
    >> composite control). You can't have the chic without the chicken, or am I
    >> missing something out of this world?
    >>
    >> Emil
    >>

    >
    >
     
    ~~~ .NET Ed ~~~, Apr 4, 2005
    #3
  4. ~~~ .NET Ed ~~~

    Teemu Keiski Guest

    Well,

    as a bit hack, you could always try like this:

    1. Get reference to System.Web.HttpContext.Current.Handler (or just straight
    via Control's Context property)
    2. Cast the returned IHttpHandler to System.Web.UI.Page and use it ;-)

    It is ugly as hell, but should work if you need it. :)

    --
    Teemu Keiski
    ASP.NET MVP, Finland


    "~~~ .NET Ed ~~~" <> wrote in message
    news:...
    >I was simply trying to get the Context.
    >
    > "Teemu Keiski" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> Can you determine at which exact stage your CreateChildControls gets
    >> called? (Tracing can help) Page property is set to a Control at Init
    >> stage of the lifecycle, and if CreateChildControls runs before that,
    >> answer is the null reference as you see.
    >>
    >> One design reason being here is that child controls should get added in
    >> CreateChildControls despite it being a postback or not, and the needs
    >> what to accomplish is it a postback or not, is done elsewhere/with
    >> different means. For example with databound controls CreateChildControls
    >> does what is needed when control renders itself from a ViewState without
    >> a datasource (actual control creation is done in DataBind method, when
    >> controls are created from datasource and certain view state data is set).
    >>
    >> Can you tell what youi are trying to do with the Page from the control?
    >> Perhaps registering a script block?
    >>
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, Finland
    >>
    >> "~~~ .NET Ed ~~~" <> wrote in message
    >> news:...
    >>> That is exactly what I have noticed, my web custom (composite) control
    >>> gets instantiated and initialized on the page, so I see that
    >>> CreateChildControls() is called as I am debugging it. What I find
    >>> strange is that I see that while within CreateChildControls() the
    >>> "this.Page" property is undefined!
    >>>
    >>> That gives me a few problems, namely that I cannot determine if I am in
    >>> a PostBack or not, if I use this.Page.IsPostBack I get an "object
    >>> reference not initialized". Or is there a way for a composite control to
    >>> determine if it is being posted back? Anyway, concerning this particular
    >>> item, I don't see the logic of having Page undefined, after all the
    >>> parent page object exists and is actually creating its child controls
    >>> (including the composite control). You can't have the chic without the
    >>> chicken, or am I missing something out of this world?
    >>>
    >>> Emil
    >>>

    >>
    >>

    >
    >
     
    Teemu Keiski, Apr 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Tam
    Replies:
    1
    Views:
    739
    John Saunders
    Aug 17, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,970
    Smokey Grindel
    Dec 2, 2006
  3. Colin Young

    CreateChildControls not being called

    Colin Young, Nov 24, 2003, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    220
    Colin Young
    Nov 26, 2003
  4. Chad
    Replies:
    0
    Views:
    224
  5. ~~~ .NET Ed ~~~
    Replies:
    1
    Views:
    214
    ~~~ .NET Ed ~~~
    Mar 25, 2005
Loading...

Share This Page