Link to home
Get AccessLog in
Avatar of brettr
brettr

asked on

Does this clear ViewState?

In asp.net 2.0, I load a user control dynamically using LoadControl().  The user can do postbacks in this control.  I use ViewState[] to keep track of several variables.   At some point, the aspx page will Remove() the user control and completely unload it.  Does this clear out the ViewState[] variables used by the user control?  If not, what is the best way to clear it?

Thanks,
brettr
Avatar of CooPzZ
CooPzZ
Flag of Australia image

I would assume no. unless in the usercontrol you are clearing up is using a dispose methods to clean up.  Just remember it acts like the page with viewstate.

but because the viewstate is just page related cache (I dont have VS here but I think you just use the .remove on the item in viewstate to clear.)
Avatar of brettr
brettr

ASKER

How do I call Dispose?  I added the method but it isn't ever hit.  

I've been getting around that by wiring up Unload().
yes in the unload will work.  just remeber that unload is called on every post back cycle, so you need to know when to dispose when calling the removes from viewstate. so the other way is to call the created dispose method and call it from the containing page when you nolonger want the control.

I think the reason dispose isn't added to the usercontrol by default because as soon as the page request is done it's removed from memory anyway, & the viewstate is gone as soon as you move to a different page...

Anyway I don't know if that really helped, either way you have to manually do the calls to clear ViewState items or you browse them away from the page.
Avatar of brettr

ASKER

Yes, UnLoad() was running multiple times.

I've added a public method to the control and put all of the code from UnLoad into it.  Before the aspx page unloads the control, I call this cleanup method.  I can step through the code and see it is running through the cleanup.  This seems to be the best way for clearing out the control's ViewState.  Guess I'm asking if you agree since I don't really know how to verify if those specific ViewState variables have actually been removed from the aspx page.

Probably one way to know if view the ViewState in page source before control load and after control unload.
ASKER CERTIFIED SOLUTION
Avatar of CooPzZ
CooPzZ
Flag of Australia image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of brettr

ASKER

Yes, I know how to use the locals.  Thanks for all of the help.