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.