In ASP.Net, during which page lifecycle event does viewstate get loaded? - asp.net

I know it happens sometime before Load, but during what event exactly?

It's loaded into memory between init and load. See this article for a full break down of the page lifecycle.

I once got into this question too and got my answer from TRULY understanding Viewstate article, which I highly recommend.
After reading it I designed a graphic that helped me to understand better what was happening on between each stage and when and how ViewState was doing its job.
I'd like to share this graphic with other people that (like myself) need to see how stuff work in a more visual way. Hope it helps! :)
Click on the image to view at full width.

That is to say, viewstate is loaded between the OnInit() and OnLoad() events of the page.
My favorite article on dealing with viewstate, which answers every question I have every time: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

You can see from the page life cycle as explained on MSDN
That the view state is loaded during the Load phase of the page lifecycle, i.e. the LoadViewState method of the "Page methods" and the LoadViewState method of the Control methods, above.

The Viewstate is actually loaded in the OnPreLoad event of the page,Just after the Page_InitComplete.

The viewstate is actually loaded between initComplete and Preload events.Check this for details http://msdn.microsoft.com/en-us/library/ms178472.aspx

Related

Preventing page lifecycle for one control

I'm using a file manager-type WebControl that does lots of postbacks. It's placed inside a Page that is relatively complex. I would like to prevent the WebControl from causing the whole Page to go through the lifecycle. An UpdatePanel helps a little, but not enough.
Is there any way to isolate the WebControl from the rest of the Page? The only way I can think of is sticking the WebControl in a separate Page and creating an iframe in the original Page. Unfortunately that also means my WebControl properties/settings are no longer in the original Page. If I want two instances of the WebControl with different settings, then I have to create a Page for each setting and reference the correct one in my iframes. Not quite as "drag & drop" as I would like. Any other suggestions?
Hard to tell, you can't prevent a control from going through lifecycle; is there anyway to identify though, that during a certain page postback, you prevent the code from running in each event handler by doing something like:
if (_shouldNotRun == true)
return;
//Event handler code
Essentially, figuring out some way to indicate whether the control should run may be an option. IFrame would work, but yes you have to deal with the issues you mentioned. Can you give more detals to the problem?
HTH.
Not 100% sure what events possible to override that are called on PostBack. A good source for the Life Cycle of a page (http://msdn.microsoft.com/en-us/library/ms178472.aspx)
But it sounds as it would be better to remake your control to create Ajax webservice requests for the functions that are possible to prevent most of the postback's?
Cheers,
Stefan

What happens between Page.PreLoad and Page.Load events?

Question is pretty straight forward.
Is there a technical reason for the existence of Page.PreLoad or is this just convenience to have a place where you can neatly place code that always have to be executed before the Load code?
Is there a difference between adding code in the PreLoad event handler and adding code at the top of the Load event handler?
And what would be a typical scenario where you use PreLoad?
Thanks!
What happens between Page_PreLoad and Page_Load is that all the other PreLoad event handlers (not just the handler(s) you wrote) have a chance to run. There is no reason to put code in Page_PreLoad. You see, chances are that you do want to be sure all the other PreLoad event handlers have fired (I'll explain in the last paragraph). Plus, by using Page_Load instead of Page_PreLoad, you give control adapter authors a chance to override the behavior of your implementation.
The purpose of the Page.PreLoad event (as far as I can tell) is to provide a hook for control authors. The behavior of the load phase of the page lifecycle, is that the Load event is raised on the page before it is raised on all its child controls. As a control author, you might want to perform some action after viewstate is loaded (so Init is too early), but before Page_Load is called (so Load is too late). How you do that is to add an event handler to Page.PreLoad.
Some of the built-in ASP.NET data binding controls use this hook to be able to auto-magically re-DataBind themselves when you update the control in certain ways after its viewstate has been loaded. Using a flag set in Page.PreLoad, a control can distinguish between changes you make in Page_Init and changes you make in Page_Load. If you implement Page_PreLoad and you don't take care to avoid touching any control that hooks PreLoad in this way, you're going to see undefined behavior because you don't know whether PreLoad is firing on the control or on the page first. To avoid this complication, always use Page_Load instead, as there is no reason not to.
I'm sure I've answered this a few times, but you're best bet is to have a thorough read thorugh the ASP.NET Page Lifecycle Overview from Microsoft and the ASP.NET Page Lifecycle explanation from 15 Seconds.

Page.Request behaviour

I have a page and few controls. I'm doing a normal postback.
On InitializeCulture event of the page the Page.Request object contains e.g. controls with their values - and that's great.
But on the other hand, when I'm trying to access this collection on the Page_Load or OnInit events, it's way smaller and doesn't have any of the controls that have been there before.
Can anyone tell me what happens with Page.Request between these events?
EDIT:
Thanks guys, I was aware of the Page Life cycle term:) and these links were indeed helpful.
I probably haven't pointed that out clearly , but:
inside override method for InitializeCulture() I Page.Request is full of various controls. Right after calling the base.InitializeCulture() , Page.Request has only server variables. I could look for values of my controls here, but can't do it - the controls are not initialized yet (so calling Request.Params.Get(SomeControl1.UniqueID) throws error)
overriding PreInit, Init or Page_Load doesn't help at all.
So the question is what and when happens with Page.Request between InitializeCulture() and next events that makes it smaller?
Btw. I find http://i.msdn.microsoft.com/dynimg/IC386473.png a much better illustration of Page Life Cycle.
EDIT:
What a mistake. Someone was doing a redirect which was resetting the whole Request collection... Lame of me. I would delete this post, but cannot.
Basic page life cycle will answer your question
Full article: http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx
Image Source: ASP.NET application and page life cycle, by Shivprasad Koirala, 19 April 2010

dynamically adding controls in asp.net page

I am adding controls dynamically in my webpage.
I add them in onload method.
everything is working fine..
But I m a bit confused about how it works..
I have read in so many articles that all controls get their values from viewstate before load event. Then how my dynamically added controls get their values when i am adding them in OnLoad event ie after LoadPostData event.
Load them in Page_Init()
Review the page lifecycle for more info:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
This is a helpful article as well:
http://www.code-magazine.com/article.aspx?quickid=0305101&page=2
In a typical GET request, the controls are created at Page_Init. Since these dynamic controls are not part of page markup, so in POST BACK, you need to recreate. Be sure that when recreating, it must have same ID otherwise your events/values will not preserve.
Dynamically added controls play catch up in the control lifecycle. Even if you add a control after it has missed the LoadViewState event, that event will still occur for the control at the time when the control is added to the page. I would suggest that any poor souls who have not had the privilege of reading this article, do so immediately:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
Source:
Professional programmer, I do not claim to be an expert, but I have read tons of articles about ViewState. :)

What is Page.LoadComplete meant for (in practice)

In the ASP.NET Page LifeCycle there is the Page.LoadComplete event.
The MSDN documentation says 'Use this event for tasks that require that all other controls on the page be loaded'. What exactly might this be? What would a 'best practice' say that LoadComplete should be used for?
When you hook up several methods to the Load event you don't know the order they will get called. So you have the PreLoad, Load and LoadComplete events to have some more control as to in what order your code gets called.
The use of this might not always be clear when working only with your page class. When working with controls however, with the need to have some code running at Load time, you might want to make it run right before or right after what would normally happen in the Load event. Then you can use the PreLoad and LoadComplete methods.
As simon mentioned, the same pattern is followed with the Init event...
More information can be found in this MSDN article about the ASP.NET Page Lifecycle
EDIT: It seems, from the MSDN article that LoadComplete is called AFTER your control events like Button.Click have been raised.
Say you have multiple controls that prepare some data in the load event. If you want to take action on that data in the load step of the ASP.NET lifecycle you need to have a way to execute after all the other load's have run. Hence the "load complete". There's also an "init complete".
Here is one example where I was trying to use Page_Load and someone correctly pointed out that LoadComplete would be better.

Resources