Trying to store items in the viewstate... wont work

T

Tarun Mistry

Hi all, im attempting to use the ViewState instead of the hidden values
normally used in procedural web development, however i cant seem to get
anything to add into it or load correctly from it.

I am trying to navigate through 4 stages of my script and saving data as i
go along. Typically i would use hidden variables however as there are no
server controls for these built in, i presumed they shouldnt be used as I
have no way of setting there values programatically (i dont think anyway).

I have a custom user control, within it at some point i execute the
following:

ViewState.Add("temp", "testing");

Then within my main page the following returns null pointer errors:

string test2 = ViewState["temp"].toString();

I have tried loading the viewstate after the page is rendered (i.e. set the
viewstate, render the page, click a button and see if my data is saved when
it rendered again [refreshing the view state]). Again doesnt work, same
problem.

I would really appreciate a simple way of doing this that would
automatically flush the data once the user has left the page. i.e. caching
or sessions arent really what I am looking for.

Thanks for any help.
Taz
 
T

Tarun Mistry

Update:

If i move the ViewState.Add("temp", "testing"); line into my main page,
everything now works. Why isnt the view state set from within the
usercontrol? :S

Thanks everyone,
Taz
 
R

Ravi Ambros Wallau

Dumb question:
Is ViewState of the control enabled?
Did you try to use Page.ViewState instead of using the ViewState property
that belongs to the control?
 
T

Tarun Mistry

Did you try to use Page.ViewState instead of using the ViewState property
that belongs to the control?

Hmm i was under the impression the ViewState was global across the entire
page and its contents. I will definately look into this!

Thank you.

Kind regards
Taz
 
R

Ravi Ambros Wallau

Well, I'm not really sure about that, but I think you should try!
Did it work?
 
T

Tarun Mistry

Appologies for the late reply, it did not work.

The ViewState is only available on the main page, not the custom user
controls, so you need to raise events and create custom properties to send
data back to the main page.

Kind regards
Taz
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top