How do I enable ViewState for child custom controls when disabled in parent?

左心房为你撑大大i 提交于 2019-12-10 14:07:42

问题


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?


回答1:


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.




回答2:


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!




回答3:


Apparently this is possible with Asp.Net 4.0



来源:https://stackoverflow.com/questions/175252/how-do-i-enable-viewstate-for-child-custom-controls-when-disabled-in-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!