Control is null in Page_PreInit

V

Vin

Hi,

I have some odd behavior in my PreInit.
A control on my page is null at the time of PreInit so I can't give it
the values it needs for it's Init, it is valid by the time of the Init
on the page but that is too late.

So here is the detailed description:
I have a Web Application with a page, Page1.
Page1 has a custom control, MyWizard, added to it which I use to wrap
the standard wizard control.
MyWizard is added statically to Page1.aspx (appears in
Page1.aspx.designer.cs)

I was using MyWizard::page_Load to dynamically create Controls and add
them to dynamically created WizardSteps.

I hit some problems recently and after reading a bit discovered that
dynamic controls should be added using the Page_Init event.

So I added a Page_PreInit handler to Page1 to set the controls data.
When I try and access MyWizard in Page_PreInit it is null!


I have tried this is a simple test project and the controls are valid
in PreInit so this seems to be non-standard behavior.
I am stuck, I can't use PreInit to initialise my Control but I have to
use it or the dynamic controls don't behave properly.

Has anyone encountered this?
Or have any suggestions on how to track it down further?

Thanks, Vin
 
V

Vin

The problem with Init/Load is that hooking up events doesn't work
properly in the Load.
I use some 3rd party controls that rely on a ScriptManager and if I
create them in Load they fail.
I did some reading and MS recommend dynamic controls be created in
Init if you want events so it isn't just a 3rd party bug.

The standard controls work fine because I don't use their events so I
was using Init/Load but I now need the Ajax enabled controls and
that's where the problem arises.

The control shouldn't be null in PreInit though.
 
B

bruce barker

all controls are dynamic in asp.net. when the aspx page is compiled it
generates code to create each control and add to control collection. if
controls have children, they may not created until later.

your wizard control should add its children in CreateChildControls. before
accessing a controls children, you should call it EnsureChildControls method.

-- bruce (sqlwork.com)
 
S

Scott M.

bruce barker said:
all controls are dynamic in asp.net. when the aspx page is compiled it
generates code to create each control and add to control collection. if
controls have children, they may not created until later.

I think you mean that controls are dynamic in ASP .NET Web "Sites". In ASP
..NET Web Application Projects, there is a .designer.vb/cs file where the
auto-generated code is placed at design-time.

-Scott
 
V

Vin

Hi,

Just to expand slightly on what Scott said.
A static control is one that is added at design time (i.e. in the
designer.vb/cs file and on the aspx page).
A dynamic control is one that is added at run time (i.e. through
code).
All controls in ASP.Net are dynamically rendered, they are not all
dynamic.

On to your suggestion.
CreateChildControls is fired after Page_Load() so it is too late.
The MSDN does describe the function as if all controls should be added
in it but if I do so the wizard control will throw a quite friendly
error saying that
"For dynamically added views, make sure they are added before or in
Page_PreInit event"
So it is no use to me.

Thanks anyway, Vin
 
S

Scott M.

I'm actually not sure that I would have explained it that way...

With ASP .NET Web Sites, there is no design-time auto-generation of control
code. The code is dynamically generated from the declarative statements in
the .aspx page at page request time by the first caller.

ASP .NET Web Application Projects have a .designer.vb/.cs file where code
for controls is generated at design-time.

I would not use terms like "static" control or "dynamic" control, as this is
misleading.

-Scott


Hi,

Just to expand slightly on what Scott said.
A static control is one that is added at design time (i.e. in the
designer.vb/cs file and on the aspx page).
A dynamic control is one that is added at run time (i.e. through
code).
All controls in ASP.Net are dynamically rendered, they are not all
dynamic.

On to your suggestion.
CreateChildControls is fired after Page_Load() so it is too late.
The MSDN does describe the function as if all controls should be added
in it but if I do so the wizard control will throw a quite friendly
error saying that
"For dynamically added views, make sure they are added before or in
Page_PreInit event"
So it is no use to me.

Thanks anyway, Vin
 
V

Vin

Hi,

It's probably a fair point that they could be confusing terms but they
are the terms Microsoft use in their documentation so I didn't expect
them to cause confusion.

That is only a side issue anyway, it would seem that no one else has
encountered my problem or if they have they have been able to use Init/
Load to get around it!

Are there any other news groups that might be worth posting this
question to?

Thanks, Vin
 
V

Vin

Ok, just in case anyone finds this post and has the same problem I'll
post the solution.

In my case I didn't mention that I was using a master page because I
didn't realise it mattered.

The master page isn't loaded until after PreInit so you can't access
it's content in PreInit.
There is a workaround for this.
Add this line to the PreInit event before trying to access any
controls
MasterPage master = this.Master;
This forces the master page to load and everything will work as
expected after that.

Read "http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-
and-preinit.aspx" for more info.

Vin
 
Joined
Sep 4, 2007
Messages
2
Reaction score
0
Right on!!

Man, what a pain!! So glad I was able to find your solution, seems to have worked. Thanks Vin!
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top