load order question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys

Ok, i have an aspx with a user control. In the user controls pageload i set
a property (mystring = "test";). i then want to access that from the
page_load of the aspx thats using the control.

the problem is that the pageload for the page fires before the pageload for
the control, so if i say in my pageload

response.write(control.mystring);

i get blank, because the controls pageload hasnt fired and set the variable
yet.

how do i get round this problem?!!

much help appreciated.

cheers


dan
 
Dan,

I might try to move the code you have in the page load event for the page
into the PreRender event of the page. Therefore, executing after the page
load event for all controls and usercontrols.

HTH
-Chris
 
Correct me if I wrong, not that I haven't used the Page Init event because I
have, but I hesitate to use in case I might need ViewState. ViewState gets
loaded somehwhere between the Page Init event and the Page Load event and
sometimes, I have had problems with not being able to access control
properties because that that fact.

That is why, I recommend using the PreRender event for the page since it
needs to access properties of the UserControl after the UserControl loads.

-Chris
~
http://weblogs.austinspad.com/caustin
 
Hi guys

I think I'm gonna try the PreRender event, basically because I'm not
declaring any controls at runtime, they've all been designed with the editor.

I know the PageLoad is firing before the control PageLoad, so logically
PreRender is the next event after that.

One question though.. am I ok "doing stuff" in the PreRender event? The
variable im getting from the control is an sql statement, so obviously once
ive got that, I need to be able to do my SQL connection stuff / databinding
etc.

Is this safe to do in PreRender?

Cheers


Dan
 
Dan, Prerender can be a great place to do "stuff." The big thing to remember
is that between Page Load and Page Prerender is where all the events (e.g.
button clicks) are processed, which may or may not be a good thing in your
case.

There's a great article by Peter van Ooijen here:
http://www.dotnetjunkies.com/Tutorial/B8550E4B-B8F5-4446-B065-0B19F4C739BC.dcik
From Mr. van Ooijen's article: "In a lot of example code a lot more happens
here [Page Load], like reading data from the database and binding the data to
the grid. I will not do that yet. The eventhandlers of the controls, like
button-clicks might update the data. It is a lot more efficient to wait to
the PreRender event. When that event is fired all handlers of button clicks
and the like have executed. The page is now ready to start writing the actual
response which will be sent to the user. It is the last opportunity to update
the components." His explanation of the order of events was a major light
bulb at least for me, so you might check it out.

I'd still suggest that if you're doing "initialization" type stuff in your
user control that it belongs in Init, but Chris is certainly right about
viewstate (the normal case is that you want to initialize your controls
*before* viewstate is restored).

It's hard to tell from your test example whether you should be doing
Init-->Load or Load-->Prerender, but don't be afraid of either, it's more
just understanding the implications of each.

hth,

Bill
 
Cheers Bill - great article and a very useful post! I'm now not frightened to
put my "stuff" in PreRender :o)

Bill Borg said:
Dan, Prerender can be a great place to do "stuff." The big thing to remember
is that between Page Load and Page Prerender is where all the events (e.g.
button clicks) are processed, which may or may not be a good thing in your
case.

There's a great article by Peter van Ooijen here:
http://www.dotnetjunkies.com/Tutorial/B8550E4B-B8F5-4446-B065-0B19F4C739BC.dcik
From Mr. van Ooijen's article: "In a lot of example code a lot more happens
here [Page Load], like reading data from the database and binding the data to
the grid. I will not do that yet. The eventhandlers of the controls, like
button-clicks might update the data. It is a lot more efficient to wait to
the PreRender event. When that event is fired all handlers of button clicks
and the like have executed. The page is now ready to start writing the actual
response which will be sent to the user. It is the last opportunity to update
the components." His explanation of the order of events was a major light
bulb at least for me, so you might check it out.

I'd still suggest that if you're doing "initialization" type stuff in your
user control that it belongs in Init, but Chris is certainly right about
viewstate (the normal case is that you want to initialize your controls
*before* viewstate is restored).

It's hard to tell from your test example whether you should be doing
Init-->Load or Load-->Prerender, but don't be afraid of either, it's more
just understanding the implications of each.

hth,

Bill

Dan Nash said:
Hi guys

I think I'm gonna try the PreRender event, basically because I'm not
declaring any controls at runtime, they've all been designed with the editor.

I know the PageLoad is firing before the control PageLoad, so logically
PreRender is the next event after that.

One question though.. am I ok "doing stuff" in the PreRender event? The
variable im getting from the control is an sql statement, so obviously once
ive got that, I need to be able to do my SQL connection stuff / databinding
etc.

Is this safe to do in PreRender?

Cheers


Dan
 
Back
Top