假设这么一种情况:
1、GridView第一次显示。
2、用户Postback页面。
3、那么在页面生存期的SaveViewState期间到底保存了什么? 难道他会把所有ViewState数据全部存起来吗?
4、下一个PostBack发生时页面生存期的LoadViewState里又Load了什么?细节问题,能不能举个例子说明一下呢?

解决方案 »

  1.   

    上面写错了,难道他会把所有GridView数据全部存起来吗? ViewState应该是指保存对某些控件属性的Programmatic Change。那上面这种情况?总之有些想不通。
      

  2.   

    In the save view state stage of the ASP.NET page life cycle, the Page class recursively iterates through the controls in its control hierarchy, invoking each control's SaveViewState() method. The GridView stores all of its contents in the view state.The GridView stores its contents in the view state so the page developer doesn't need to rebind the database data to the GridView on each and every page load, but only on the first one. The benefit is that the database doesn't need to be accessed as often. If, however, you set a GridView's EnableViewState property to false, you'll need to rebind the database data to the GridView on both the first page load and every subsequent postback.你地明白?
      

  3.   

    如果和服务器没有交互,我是不启用ViewState的
      

  4.   

    所有属性都要存起来.
    举个例子.
    你在Page_load的if(!this.IsPostBack)中给gridview 指定一个
    DataSource,绑定 然后页面上放个按纽 
    点击一下,那么这个gridview还呈现在页面上,与第一次无异.然后你禁用掉ViewState再试.
      

  5.   

    保存了控件的状态,如果你每次GridView的数据都是从数据库来的,就可以禁用ViewState
      

  6.   

    谢谢楼上几位,按cpp2017的方法也试了一下,这回总算有些明白了。
      

  7.   

    GridView有很多的地方需要ViewState,随便就可以举出几十个。例如你随便设置7、8个Style集合中的一个,例如设置HeaderStyle集合中的某一项,或者设置一下PageSize属性,或者设置一下DataKeyNames,等等,你仅需要在某个事件中设置一次,控件自己就记住了状态值。你可以在页面上随便放一个Button并引发回发,你可以看到GridView逻辑正确,不会因为页面上其它某个地方的交互而丢失自己的状态值。
      

  8.   

    回答你的标题中提出的问题:为什么许多书上说“禁用ViewState会提高效率”?其实这是废话,如果什么都不做,“效率最好”,但是功能出不来了。所以废话永远不会犯错,把选择的责任推给你自己去权衡。
      

  9.   

    在EnableViewState为true的情况下,在SavaViewState期间会把属性值的改变存进ViewState里,然后在下一个PostBack的页面生存期的LoadViewState期间再读出来替换掉declarative up里设置的原始值,这个我理解了。thank you, sp1234。还有就是EnableViewState=true能减少数据库访问次数,但同时加大了下载量,还有post操作的时间。
      

  10.   

    在实际应用中我经常设置显示提示信息的Label EnableViewState=false,因为这个信息每次都会被修改。而对于像GridView或者DropDownList这样的控件,禁用视图状态给自己找麻烦的概率更大
      

  11.   


    http://topic.csdn.net/u/20080804/11/ef6862d7-a199-453b-9331-6163035ddb50.html
      

  12.   

    现在剩下的就是ControlState了,这东西和ViewState又有什么区别?ViewState能够存储控件属性的改变,那还要什么ControlState呢?这问题不用回答,照样放分,只是问问。
      

  13.   

    ControlState可以理解为和ViewState差不多的东东,但是是控件正常工作必须的最基本的必要条件,所以不受EnableViewState=false的影响?
      

  14.   

    ---------------------------------------------------
    实在
    一些只显示数据,而不需要交互的控件就禁用ViewState。
      

  15.   

    Control State:ControlState is similar to ViewState but cannot be turned off.You need to override the 
    SaveControlState and LoadControlState methods to access ControlState.
    It is not enabled by default and you must call the RegisterRequiresControlState method of the Page to enable it for a control.
      

  16.   

    viewState上面的几位已经说的很清楚了.
      

  17.   

    关于ViewState的实质性内容,可以参看
    MSDN上的这个页面
    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_aspnetctrlauth/html/9e98c7de-a888-48df-b14e-02ec8bef7681.htm它对启用和禁用viewstate做了详细的对比.