473,386 Members | 1,860 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,386 software developers and data experts.

Access ViewState in Page Init

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?

Jul 24 '06 #1
9 11266
Alexander van Doormalen wrote:
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.

--

Riki
Jul 24 '06 #2
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...

Jul 24 '06 #3
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.

Jul 25 '06 #4
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.
Jul 25 '06 #5
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/2...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.

Jul 25 '06 #6
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.

Jul 26 '06 #7
Hi,
I just read this article:
http://infinitiesloop.blogspot.com/2...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.
great article. Even if it does not solve our problems, it gives a deeper
insight in how asp.net works. Thanks a lot.

Have you solved your problem with the given information?

Volker.
Jul 26 '06 #8
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

Jul 26 '06 #9
Alexander van Doormalen schrieb:
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.
Jul 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
4
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
0
by: seven | last post by:
I have a base page with an HTMLForm object (added to the base page control heiarchy during Init). In the design of pages derived from this base page, I can choose to create controls and add them...
0
by: Matt Howeson | last post by:
I have a problem with a usercontrol that is dynamically loaded, from a web control and is not restoring it's viewstate properly. Here's the scenario. Within our content management system, I have...
2
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
1
by: Graham | last post by:
I have created a server control that reads data from an xml file and renders either a datagrid of entries from a database or group of form controls to allow editing of an individual entry from the...
1
by: Nathan Sokalski | last post by:
I have a problem that is driving me crazy. I have a User Control composed of three DropDownLists that will be used to select a date. I have everything working except for one thing. When I select a...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.