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

page.init vs page.load

P: n/a
UJ
I've got a page with a user control on it. While the page is loading, it
needs to check certain conditions of the user object to enable/disable
things on the screen. Currently in the page_load of the user control I do
things like read from the database on the first load, set some internal
variables, ...

If I need those things to be available to me on the page load, should I do
them on page_init? Are there any ramifications of using page_init? Does it
work pretty much the same way as page_load except that it happens before
page_load?

TIA

Jeffrey.
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi
Yes Page_Init is pretty much similar to Page_Load. You can do all the things
which You did in Page_Load. But the major difference is Page_Load will be
fired each time when the page submits. Page_Init will be fired only once when
the page loads for the first time.

Hence please decide accordingly and write your code.
Hope this info might give you some idea. If I am wrong please let me know.

--
Srinivas Nadella
SSE - Satyam Computer Services
Hyderabad
India
"UJ" wrote:
I've got a page with a user control on it. While the page is loading, it
needs to check certain conditions of the user object to enable/disable
things on the screen. Currently in the page_load of the user control I do
things like read from the database on the first load, set some internal
variables, ...

If I need those things to be available to me on the page load, should I do
them on page_init? Are there any ramifications of using page_init? Does it
work pretty much the same way as page_load except that it happens before
page_load?

TIA

Jeffrey.

Nov 19 '05 #2

P: n/a
Page_Init and Page_Load will both be fired every time the page is executed,
either through a postback or not a postback. If you are not having any
problems loading your page then I would keep all your stuff in Page_Load.
You loading data from the database and turning things on and off seem like
they are part of the "loading" process.

bill
"Srinivas Nadella" <Sr*************@discussions.microsoft.com> wrote in
message news:19**********************************@microsof t.com...
Hi
Yes Page_Init is pretty much similar to Page_Load. You can do all the things which You did in Page_Load. But the major difference is Page_Load will be
fired each time when the page submits. Page_Init will be fired only once when the page loads for the first time.

Hence please decide accordingly and write your code.
Hope this info might give you some idea. If I am wrong please let me know.

--
Srinivas Nadella
SSE - Satyam Computer Services
Hyderabad
India
"UJ" wrote:
I've got a page with a user control on it. While the page is loading, it
needs to check certain conditions of the user object to enable/disable
things on the screen. Currently in the page_load of the user control I do things like read from the database on the first load, set some internal
variables, ...

If I need those things to be available to me on the page load, should I do them on page_init? Are there any ramifications of using page_init? Does it work pretty much the same way as page_load except that it happens before
page_load?

TIA

Jeffrey.

Nov 19 '05 #3

P: n/a
UJ
The problem I'm having is that this is in a user control that I'm doing the
page loading stuff. But the page when it loads needs things that are set in
page load of the user control. As far as I can tell everything happens in
the page load, then everything in the page load for the user control. But
there are things that in the main page load that are dependent on the user
control.

The only way I can see around it is to use session variables in the user
control which will then be available to me in the main page before the user
control gets loaded.

TIA - Jeffrey.

"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Page_Init and Page_Load will both be fired every time the page is
executed,
either through a postback or not a postback. If you are not having any
problems loading your page then I would keep all your stuff in Page_Load.
You loading data from the database and turning things on and off seem like
they are part of the "loading" process.

bill
"Srinivas Nadella" <Sr*************@discussions.microsoft.com> wrote in
message news:19**********************************@microsof t.com...
Hi
Yes Page_Init is pretty much similar to Page_Load. You can do all the

things
which You did in Page_Load. But the major difference is Page_Load will be
fired each time when the page submits. Page_Init will be fired only once

when
the page loads for the first time.

Hence please decide accordingly and write your code.
Hope this info might give you some idea. If I am wrong please let me
know.

--
Srinivas Nadella
SSE - Satyam Computer Services
Hyderabad
India
"UJ" wrote:
> I've got a page with a user control on it. While the page is loading,
> it
> needs to check certain conditions of the user object to enable/disable
> things on the screen. Currently in the page_load of the user control I do > things like read from the database on the first load, set some internal
> variables, ...
>
> If I need those things to be available to me on the page load, should I do > them on page_init? Are there any ramifications of using page_init? Does it > work pretty much the same way as page_load except that it happens
> before
> page_load?
>
> TIA
>
> Jeffrey.
>
>
>


Nov 19 '05 #4

P: n/a
As mentioned by someone else, Page_Init and Page_Load fire for every request
into the page. The main difference is that in Page_Init upon a postback,
the controls have not yet been loaded with the post data. It's rare to need
to do work that early in the page. Page_Load is where you typically do your
initializtion work on the page. Having said that, the name Page_Init can
be misleading.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I've got a page with a user control on it. While the page is loading,
it needs to check certain conditions of the user object to
enable/disable things on the screen. Currently in the page_load of the
user control I do things like read from the database on the first
load, set some internal variables, ...

If I need those things to be available to me on the page load, should
I do them on page_init? Are there any ramifications of using
page_init? Does it work pretty much the same way as page_load except
that it happens before page_load?

TIA

Jeffrey.


Nov 19 '05 #5

P: n/a
Ben
I mainly use page init for just that, initilization. What's missing from
this thread is LoadViewState. Page_Init fires before LoadViewState does.
If for example you happen to have a dynamically loaded control on your page
and you want it to be reloaded from viewstate, you must recreate it in
Page_Init.

The short and simple approach to page life cycle I use is as follows.

Page_Init (Runs during every page request)
LoadUserControls
Default page level variables, instantiate page level classes etc

LoadViewState (Only runs during postback)
Overwrite the default values in page variables
I only read from viewstate in LoadViewState.

Page_Load (Runs during every page request)
I rarely use page_load

Changed Events (Only runs during postback)
I update any page level variables or objects affected by the change

PostBackEvents (Only runs during postback)
I update any page level variables or objects affected by the change

Page_PreRender (Runs during every page request)
Ok, here is the ASP.NET page lifecycle work horse. This event fires
after all informative events have fired. You know if the page number has
changed, you know if textboxes have changed and you know why the page posted
back.

This is where I do my databinding if necessary and most page logic.

SaveViewState (Runs during every page request)
This is where I save any values to viewstate that need to be saved.
I only save to view state in SaveViewState

Dispose (Runs during every page request)
This is where I clean up any page level variables etc

HTH,
Ben

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:83**********************@msnews.microsoft.com ...
As mentioned by someone else, Page_Init and Page_Load fire for every
request into the page. The main difference is that in Page_Init upon a
postback, the controls have not yet been loaded with the post data. It's
rare to need to do work that early in the page. Page_Load is where you
typically do your initializtion work on the page. Having said that, the
name Page_Init can be misleading.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I've got a page with a user control on it. While the page is loading,
it needs to check certain conditions of the user object to
enable/disable things on the screen. Currently in the page_load of the
user control I do things like read from the database on the first
load, set some internal variables, ...

If I need those things to be available to me on the page load, should
I do them on page_init? Are there any ramifications of using
page_init? Does it work pretty much the same way as page_load except
that it happens before page_load?

TIA

Jeffrey.


Nov 19 '05 #6

P: n/a
If my containing page depends on some timing in the user control, I will not
leave it to the Page_Load event. You could move all your usercontrol's Load
code to another method.

public class myUserControl : UserControl
{
//this method will be the "page_load" for the user control.
public void LoadState();
}

In my Page_Load I will execute the LoadState method manually. Then you can
use the UC for what you need to.
public class myPage : Page
{
private myUserControl myUC;
protected void Page_Load(...)
{
myUC.LoadState();
//dependant calculated stuff
}

HTH,

bill

"UJ" <fr**@nowhere.com> wrote in message
news:ei**************@TK2MSFTNGP09.phx.gbl...
The problem I'm having is that this is in a user control that I'm doing the page loading stuff. But the page when it loads needs things that are set in page load of the user control. As far as I can tell everything happens in
the page load, then everything in the page load for the user control. But
there are things that in the main page load that are dependent on the user
control.

The only way I can see around it is to use session variables in the user
control which will then be available to me in the main page before the user control gets loaded.

TIA - Jeffrey.

"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Page_Init and Page_Load will both be fired every time the page is
executed,
either through a postback or not a postback. If you are not having any
problems loading your page then I would keep all your stuff in Page_Load. You loading data from the database and turning things on and off seem like they are part of the "loading" process.

bill
"Srinivas Nadella" <Sr*************@discussions.microsoft.com> wrote in
message news:19**********************************@microsof t.com...
Hi
Yes Page_Init is pretty much similar to Page_Load. You can do all the

things
which You did in Page_Load. But the major difference is Page_Load will be fired each time when the page submits. Page_Init will be fired only once
when
the page loads for the first time.

Hence please decide accordingly and write your code.
Hope this info might give you some idea. If I am wrong please let me
know.

--
Srinivas Nadella
SSE - Satyam Computer Services
Hyderabad
India
"UJ" wrote:

> I've got a page with a user control on it. While the page is loading,
> it
> needs to check certain conditions of the user object to
enable/disable > things on the screen. Currently in the page_load of the user control I do
> things like read from the database on the first load, set some
internal > variables, ...
>
> If I need those things to be available to me on the page load, should

I do
> them on page_init? Are there any ramifications of using page_init?
Does it
> work pretty much the same way as page_load except that it happens
> before
> page_load?
>
> TIA
>
> Jeffrey.
>
>
>



Nov 19 '05 #7

P: n/a
which you pick depends on what the code needs to do.

order of event firing

1) on init
2) load viewstate (postback only)
3) load postback data (postback only)
4) page load

if the code looks at the value of controls, it should be in page load, if
the code creates dynamic controls it should be in init. if the code initials
data values, do you want it before or after postback data?
-- bruce (sqlwork.com)

"UJ" <fr**@nowhere.com> wrote in message
news:OD**************@TK2MSFTNGP14.phx.gbl...
I've got a page with a user control on it. While the page is loading, it
needs to check certain conditions of the user object to enable/disable
things on the screen. Currently in the page_load of the user control I do
things like read from the database on the first load, set some internal
variables, ...

If I need those things to be available to me on the page load, should I do
them on page_init? Are there any ramifications of using page_init? Does it
work pretty much the same way as page_load except that it happens before
page_load?

TIA

Jeffrey.

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.