Access ViewState in Page Init

  • Thread starter Alexander van Doormalen
  • Start date
A

Alexander van Doormalen

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 LoadControl in the
Load event handler. This works fine however the user controls which
need to be added need to load their ViewState aswell as the page
itself.

In this situation this isn't gonna work cause the ViewState is loaded
before the Load event fires. Because I need to know which control to
're-add' I saved this in the ViewState. However in the Page Init event
handler you can't access the ViewState cause its not loaded yet and
this is the only place (as far as I know) which lets the added user
control load its ViewState.

Does anybody know some nice way to solve this. I thought of sessions
but this is gonna be messy when having multiply browser windows open to
the same page. Cookies will give you the same result. Maybe using
custom hidden fields but this is in my opinion nasty aswell. Is their
anyway the ViewState can be loaded in the Page Init event handler. Or
accessed/decrypted etc?
 
R

Riki

Alexander said:
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
LoadControl in the Load event handler. This works fine however the
user controls which need to be added need to load their ViewState
aswell as the page itself.

In this situation this isn't gonna work cause the ViewState is loaded
before the Load event fires. Because I need to know which control to
're-add' I saved this in the ViewState. However in the Page Init event
handler you can't access the ViewState cause its not loaded yet and
this is the only place (as far as I know) which lets the added user
control load its ViewState.

Does anybody know some nice way to solve this. I thought of sessions
but this is gonna be messy when having multiply browser windows open
to the same page. Cookies will give you the same result. Maybe using
custom hidden fields but this is in my opinion nasty aswell. Is their
anyway the ViewState can be loaded in the Page Init event handler. Or
accessed/decrypted etc?

As far as I know, ViewState IS accessible in Page_Init.
The viewstate of the page is restored AFTER Page_Init, but you
can use either Page_Init or Page_Load to recreate your dynamic
controls based on the values of the viewstate. This shouldn't
interfere with the viewstate of the page.
 
A

Alexander van Doormalen

How can you access the viewstate if it isn't loaded yet?

Anyways I just read somewhere else that if you add controls using
ControlsCollection.Add during the Page Load the viewstate is loaded
afterall. So the problem that my viewstate is empty must be something
different. Maybe it has something do with Atlas...
 
A

Alexander van Doormalen

Aldough the ControllCollection.Add should restore the ViewState I still
have the same problem that my viewstate is empty after a postback and
only for this user control.

My structure:
- Default.aspx (loads usercontrol depending on querystring in Page
Init)
--UserControl (loads usercontrol depending on some events in Page Load)
---SubUserControl --> problem control

The viewstate for the aspx page and the first UserControl works fine.
However for the SubUserControl the Viewstate is always empty.

I tried to move the SubUserControl loading in the Page Init (in
UserControl) and it resulted in a working ViewState. However the
postback information isnot restored for all WebControls (like lists,
textboxes etc).

Viewstate is enabled for SubUserControl. Also the ViewState isnot empty
after the Page Load event.

I tried to view the ViewState by decrypting it with some tools but none
of them can decrypt it like they should so I'm a bit stuck right now.
 
V

Volker Goehrke

Hi Alexander,
The viewstate for the aspx page and the first UserControl works fine.
However for the SubUserControl the Viewstate is always empty.

I tried to move the SubUserControl loading in the Page Init (in
UserControl) and it resulted in a working ViewState. However the
postback information isnot restored for all WebControls (like lists,
textboxes etc).

we're having the very same problem with asp.net 2.0 checkboxes. When
we're dynamically creating checkboxes in an event after the init-event,
the view state and form values will never be restored into the control.
It seems to me, that if you're controls depends on viewstate oder
form-value, it must be created and added to the controls collection in
the init-event.

Another odd behaviour, we discovered, is, when we are creating the
checkboxes controls in the init-event, after adding them to the
Controls-Collection we were breaking the viewstate behaviour, by for
example resorting the Controls-Collection in the prerender-event. In our
special case, we created a table with several rows, containing
checkboxes and wanted to sort the table rows in the prerender-event. We
didn't created new controls, we just took one tablerow out of the
collection and added it to the end of the collection. The output was
just fine, but the viewstate didn't worked any more.

So, are you doing something with your Controls collection? Could you
post some simple code showing the behaviour?

Volker Goehrke.
 
A

Alexander van Doormalen

I can't test anything right now but i think it has something to do with
the control ID's. Because I didn't assign them they are generated
automatically. If the order changes somehow the control ID will be
different and so ViewState won't be reloaded as the ID's won't match.

I just read this article:
http://infinitiesloop.blogspot.com/2006/03/truly-understanding-viewstate.html

It gave me some extra information what can help me solve this. Will
look into it tonight when I've got some time.
 
A

Alexander van Doormalen

Solved my problem by declaring all the required controls in the
designer. Each in a seperate View in the MultiView control. Then
everything works as supposed to if I switch the active view in/after
the Page Load.
 
A

Alexander van Doormalen

I had some help from the author of that article. He pointed me to my
final solution, which is declaring the control at all times
 
V

Volker Goehrke

Alexander said:
I had some help from the author of that article. He pointed me to my
final solution, which is declaring the control at all times

I'm very surprised, that such invaluable information about viewstate
handling is kept so secretly. I found almost nothing about it in the
book "professional asp.net 2.0" and the documentation of the
Controls.Add method and the ControlsCollection class in the msdn didn't
even mention their behaviour on events, viewstate or form data. There is
an article "Dynamic Web Server Controls and View State" in the msdn,
where in my opinion the details are left out.

Volker.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top