473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

page.init vs page.load

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
7 23306
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Mauldin | last post by:
This is a standard ASP application that has several pages at the root withthe global.asa. I set a session variable session("accountid") = "123456" within an asp page and then response.redirect to...
7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
1
by: mato | last post by:
this is what i experienced in my web application and i realy do not understand. this is my web page class behind public class MyWebPage : System.Web.UI.Page { //some page controls ..... ...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
3
by: Vadim Vulfov | last post by:
I have the single page that I would like to show multiple times in one aspx file continuously with different parameters in querystring. So, I was planning to go through the loop and pick up the...
1
by: Tony Cheng | last post by:
Hello everybody, I got a aspx which have several user controls on it. My question is that what is the order of ASP.NET execute the page and user control ? does it like this : Page Init ->...
9
by: Alexander van Doormalen | last post by:
I have a situation that user controls are dynamically loaded within a page. To know which control to 're-add' to the page I saved the control path in the ViewState. This Control is added using...
0
by: =?Utf-8?B?VGFsYWxTYWxlZW0=?= | last post by:
if i put slash("/") i mean just a / at the end of url then .aspx page couldn't load the css and images. i have a portal in .net if any body puts / at the end of url that page couldn't load the...
5
by: yarivot26 | last post by:
I'm using ODBC to read a office access db the db have almost 10,000 rows so I want to split it into pages The first page load nice and quick, but in other pages i get: Maximum execution time...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.