控件和页面视图状态之间的区别

人气:865 发布:2022-09-22 标签: c#

问题描述

控件和页面视图状态之间的区别?如果页面视图状态为False则控制视图状态是否工作?子控件视图状态是否也可以工作?

difference between control and page view state? if page view state if False then Control view state Work or Not?Can child control view state also work or Not?

推荐答案

ViewState和ControlState都是ASP中使用的机制.NET用于在回发之间维护数据。两者都保存在一个名为_VIEWSTATE的隐藏字段中。 不同之处在于: 1)ViewState可以被禁用无法禁用控制状态。 2)ViewState是通过将控件的EnableViewState属性设置为true来实现的。 即使EnableViewState关闭,控制状态仍然有效。 要使用控制状态(例如在自定义控件中),我们必须重写OnInit方法,在OnInit方法中调用RegisterRequiresControlState方法,然后重写SaveControlState和LoadControlState方法。 自定义控件代码是从WebControl派生的类中编写的。 3)控制状态仅用于小数据。 例如:即使EnableViewState关闭,也要在GridView中维护点击的页码 更多信息: ^ ] 控制状态与视图状态 ViewState and ControlState are both mechanisms used in ASP.NET for maintaining data across postbacks. Both are preserved in a hidden field known as _VIEWSTATE. The differences are: 1)ViewState can be disabled while the Control State cannot be disabled. 2)ViewState is implemented by using EnableViewState property of a control to true. Control State works even when EnableViewState is off. To use Control State (for example in a custom control) we have to override OnInit method,call RegisterRequiresControlState method in OnInit method and then override the SaveControlState and LoadControlState methods. Custom Controls code is written in classes derived from WebControl. 3)Control State is used for small data only. eg: maintain clicked page number in a GridView even when EnableViewState is off For more info : Using Control State in ASP.NET 2.0[^] Control State vs. View State

试试这个链接 view-between-viewstate - 并且控制状态 检查msdn官方网站控制状态与视图状态

对于你的两个问题,答案是否定的。如果父页面的EnableViewState设置为false,则页面上所有控件的ViewState将不起作用。父母和子女控制相同。 For the both of your questions, the answer is NO. if the EnableViewState of parent page is set false then the ViewState for all the controls on the page will not work. same for parent and child controls.

637