By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

load order question

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Initialize your user control at Page Init (not page load).

Bill

"Dan Nash" wrote:
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

Nov 18 '05 #2

P: n/a
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

"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:BA**********************************@microsof t.com...
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

Nov 18 '05 #3

P: n/a
Hi Dan,

Use the control's (or the page's) Init event.

The Page Init fires in the controls first then in the Page, then Page load
fires, then Load in the controls.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:BA**********************************@microsof t.com...
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

Nov 18 '05 #4

P: n/a
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

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
Hi Dan,

Use the control's (or the page's) Init event.

The Page Init fires in the controls first then in the Page, then Page load fires, then Load in the controls.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:BA**********************************@microsof t.com...
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


Nov 18 '05 #5

P: n/a
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

"Chris Austin" wrote:
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

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
Hi Dan,

Use the control's (or the page's) Init event.

The Page Init fires in the controls first then in the Page, then Page

load
fires, then Load in the controls.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:BA**********************************@microsof t.com...
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



Nov 18 '05 #6

P: n/a
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/Tutoria...9F4C739BC.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" wrote:
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

"Chris Austin" wrote:
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

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
Hi Dan,

Use the control's (or the page's) Init event.

The Page Init fires in the controls first then in the Page, then Page

load
fires, then Load in the controls.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"Dan Nash" <da*@musoswire.co.uk> wrote in message
news:BA**********************************@microsof t.com...
> 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


Nov 18 '05 #7

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

"Bill Borg" wrote:
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/Tutoria...9F4C739BC.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" wrote:
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

"Chris Austin" wrote:
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

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
> Hi Dan,
>
> Use the control's (or the page's) Init event.
>
> The Page Init fires in the controls first then in the Page, then Page
load
> fires, then Load in the controls.
>
> +++ Rick ---
>
> --
>
> Rick Strahl
> West Wind Technologies
> http://www.west-wind.com/
> http://www.west-wind.com/weblog/
> http://www.west-wind.com/wwThreads/
> ----------------------------------
> Making waves on the Web
>
>
> "Dan Nash" <da*@musoswire.co.uk> wrote in message
> news:BA**********************************@microsof t.com...
> > 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
>
>

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.