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

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

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
 
T

Teemu Keiski

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?
 
N

~~~ .NET Ed ~~~

I was simply trying to get the Context.

Teemu Keiski said:
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?
 
T

Teemu Keiski

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. :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top