Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First i must admit that i've never used <code>ControlState</code> to persist values across postbacks. But control developers who create Controls that are used by other developers might want to use it to ensure functionality even if <code>ViewState</code> is disabled. Otherwise disabling would result in incorret behaviour or errors that are not obvious.</p> <p>So in my opinion you would use <code>ControlState</code> if you would normally use <code>ViewState</code> but you must ensure that the control also works when <code>ViewState</code> is disabled. In this case the sentence <em>"Do not use control state as an alternative to view state"</em> would be wrong, because you should use ViewState as well only for <strong>small amounts</strong> of data that must be persisted across postbacks.</p> <p>An example to clarify what i mean:</p> <p>Consider you want to create a custom control that inherits <code>GridView</code>. You can store the data in <code>ViewState</code> to maintain values across postbacks. You should allow to disable the <code>ViewState</code> on your control without any problems, you should not rely on the fact that it's enabled, because developers who are using your control might want to reduce network traffic. But you could provide other properties that are stored in <code>ControlState</code> like <em>SortDirection</em>, <em>PageIndex</em>, <em>DeleteButtonText</em> etc. that you think are important and are single values, therefor less resource intensive than storing all <code>GridViewRows</code>.</p> <p><strong>Edit</strong>: According to your actual problem:</p> <p>You could enable ViewState for some controls and disable it for others, but you cannot disable it for whole page and enable it for child-controls in this page.</p> <blockquote> <p>If you set turn page's ViewState off, then there is no way for you to enable ViewState for specific components. This is because ViewState is serialzed recursively, so when if the Page is not allowing ViewState, it will not serialize the ViewState for any of it's child controls. If you don't want to explicitly turn ViewState off on individual controls, but want to keep some controls ViewState aware, the best way would be writing a small utility method which turns ViewState off for all controls (using recursion or otherwise). Then enable ViewState for the controls that you would like to enable ViewState for. Alternatively, a middle ground and less forceful way may possible if controls are groups inside other container controls (such as Panel). You can disable ViewState for all controls inside a Panel by disabling ViewState of the Panel.</p> </blockquote> <p><a href="https://stackoverflow.com/questions/186657/enable-viewstate-for-few-controls-and-disable-for-others-page/186691#186691">Enable ViewState for few controls and disable for others/page</a></p> <p><strong>Edit</strong>: Here is an extension to enable/disable ViewState for all child-controls(VB.NET)</p> <pre><code>&lt;System.Runtime.CompilerServices.Extension()&gt; _ Public Sub EnableChildViewState(ByVal parentControl As System.Web.UI.Control, enable as Boolean) If parentControl.HasControls Then For Each c As System.Web.UI.Control In parentControl.Controls c.EnableViewState = enable EnableChildViewState(c, enable) Next End If End Sub </code></pre> <p>You can call it:</p> <pre><code>Page.EnableChildViewState(False) MyControl.EnableViewState = True </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload