views:

1160

answers:

2

I am trying to create a custom control that a "gridview" like control but specifcally for business objects that implement certain custom interfaces.

In doing this I have come across the following problem.

I have a control that I have disabled viewstate on (and I don't want to re-enable it) and it has a child control that I want viewstate enabled on. I can't seem to get the viewstate on the child control to work since its parents is disabled. Does anyone have any ideas of how to get that to work?

+5  A: 

You cannot enable viewstate on a control which is within another control that has viewstate disabled.

Your only option is to enable it for the outer control, and then turn it off for all of the controls within it, except for the control you need viewstate.

EnableViewState property on any container will override the behavior of all controls within that containter.

Good Luck!

EDIT: You may want to look at your CreateChildContols() method and enumerate the controls disabling viewstate from there for each of the controls within the custom control using the EnableViewState property.

Jason Stevenson
Jason is correct, once it is disabled above you, you can't enable it further down.
Tom
A: 

If you're happy putting data into the ViewState manually (instead of letting ASP.NET preserve the state of your control for you), You could put items directly into the ViewState of the page, rather than the ViewState of your control.

I.e. instead of saying:

this.ViewState["someKey"] = someValue;

say:

this.Page.ViewState["someKey"] = someValue;

Be careful though - if you have more than one instance of your control on the page, you'll have to make sure they use different keys!

teedyay