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?

    ~~~ .NET Ed ~~~, Apr 1, 2005
    1. Advertisements

  2. ~~~ .NET Ed ~~~

    Teemu Keiski Guest


    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, Apr 4, 2005
    1. Advertisements

  3. I was simply trying to get the Context.

    ~~~ .NET Ed ~~~, Apr 4, 2005
  4. ~~~ .NET Ed ~~~

    Teemu Keiski Guest


    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, Apr 4, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.