简体   繁体   English

ASP.NET Server控件-控件之间是否可以共享Viewstate

[英]ASP.NET Server Control - Is shared Viewstate between controls possible

If I have a master 'composite custom server control', with several child custom controls, is it possible, for the master control to share its viewstate with the child controls, which are also custom composite server controls,(all of the controls are composite custom server controls)? 如果我有一个具有多个子自定义控件的主“复合自定义服务器控件”,则该主控件是否可以与子控件(也是自定义复合服务器控件)共享其视图状态,(所有控件都是复合控件)自定义服务器控件)?

To expand a little more, say I have a Person control, Address control, Phone control and Notes control. 要进一步扩展,请说我有一个Person控件, Address控件, Phone控件和Notes控件。 Address , Phone and Notes can exist as either independant controls, or as part of the Person control. AddressPhoneNotes可以作为独立控件存在,也可以作为Person控件的一部分存在。 Since each control is responsible for its own viewstate, and stores required data in it, so it can render/postback etc, it ends up that there is a lage duplication in the viewstate, since the Person control stores all the data, and then each child control stores its own data again. 由于每个控件负责其自己的视图状态,并在其中存储所需的数据,因此它可以进行渲染/回发等操作,最终,由于Person控件存储了所有数据,因此视图状态中存在重复标记,然后每个子控件再次存储其自己的数据。 Also, to further complicate things, the Person Control adds the child controls dynamically, so its possible to add a 2nd address/phone number/note etc,which can cause yet a larger viewstate(up to 1MB). 同样,为了使事情更加复杂, Person Control动态添加了子控件,因此可以添加第二个地址/电话号码/注释等,这可能会导致更大的视图状态(最大1MB)。

In the case of when all the Address/Phone/etc controls are children of the the Person Control, is it possible for me to somehow share common viewstate data, so I dont have 2/3/4 copies of some stuff in the viewstate, and when they are not components, just act as normal? 如果所有的Address / Phone / etc控件都是Person控件的子控件,那么我是否可能以某种方式共享常见的viewstate数据,所以我在viewstate中没有某些东西的2/3/4副本,而当它们不是组件时,只是正常运行?

I do have one idea for a solution, but its fairly nasty imo, where I could modify the Person control, expose its viewstate or the data, and then in the child control, would check the control hierarchy, and if the child control is part of a Person , dont use to its own viewstate, use the exposed one. 我确实有一个解决方案的主意,但是它相当讨厌,我可以在其中修改Person控件,公开其视图状态或数据,然后在子控件中检查该控件的层次结构,以及该子控件是否是该控件的一部分一个的视图,不要使用其自己的视图状态,而使用暴露的视图状态。 This would require me to rework alot of the existing code, which I'd rather avoid if possible. 这将需要我重做许多现有代码,如果可能的话,我宁愿避免这样做。

The first question I'd ask is, "How much of this data do you really need to keep on the ViewState?" 我要问的第一个问题是:“您真正需要在ViewState上保留多少数据?” For example, is there enough information in a typical page request to be able to rebuild all of the information that is in some of these controls? 例如,在典型的页面请求中是否有足够的信息,以能够重建其中某些控件中的所有信息?

I find that ASP.NET typically uses ViewState far more than I really need it to, and if you're getting a 1MB viewstate, I suspect the same is probably true for you. 我发现ASP.NET通常使用ViewState的次数远远超过了我真正需要的次数,并且如果您获得1MB的ViewState,我怀疑您可能也是如此。 I highly recommend reading this article to get a more full understanding of how ViewState works, what causes values to get saved to ViewState, and how to avoid using it where it's not necessary. 我强烈建议阅读本文 ,以更全面地了解ViewState的工作原理,导致值保存到ViewState的原因以及如何避免在不需要的地方使用它。 You may be able to solve the bulk of your problem simply by doing more of your work in Page_Init instead of Page_Load, for example. 例如,您可以通过在Page_Init而不是Page_Load中完成更多工作来解决大部分问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM