views:

142

answers:

2

Hello, I cretated my own server control (a dropdownlist) and thus my own LoadViewState and SaveViewState methods. When is the LoadViewState called? I added the control to my page and looked when the methods are called. Only the SaveViewState is called when the page is requested, LoadViewState is not. Do I have to call it manually?

Thanks :)

A: 

After Init, but before Load. LoadViewState does not run on the initial page load, but subsequent page loads. No need to when no state exists. No you do not need to call manually. You just need to worry about the data you want to save, and reloading that data during the loading phase.

Brian
I dont get that...so I never call LoadViewState?
grady
No, the ASP.NET framework does; you only worry about the loading and saving part; it determines the right time to call it.
Brian
+1  A: 

The diagram on this MSDN page of the ASP.NET page lifecycle is an excellent reference to have on-hand for these sorts of questions (it's printed out and taped on my cube wall right now).

As you'll see on the diagram, LoadViewState for a control is called after the page's Init, and before the page's PreLoad; it is called only on postback, not on initial page load.

A control's SaveViewState is called after the page's PreRenderComplete, but before the actual Render.

mikemanne