Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes the ASP.NET ParameterCollection (or its ParametersChanged event) depend on viewstate?
    primarykey
    data
    text
    <p>Using reflector I found the following code in the <code>System.Web.UI.WebControls.Parameter</code> class:</p> <pre><code>internal void UpdateValue(HttpContext context, Control control) { object obj2 = this.ViewState["ParameterValue"]; object obj3 = this.Evaluate(context, control); this.ViewState["ParameterValue"] = obj3; if (((obj3 == null) &amp;&amp; (obj2 != null)) || ((obj3 != null) &amp;&amp; !obj3.Equals(obj2))) { this.OnParameterChanged(); } } </code></pre> <p>The line</p> <pre><code>this.OnParameterChanged(); </code></pre> <p>is what triggers the <code>ParametersChanged</code>-event in the end, as I understand (through <code>this._owner.CallOnParametersChanged()</code> in <code>OnParameterChanged</code>).</p> <p>Now my question is: what happens if <code>EnableViewState</code> is set to <code>false</code> (e.g. on the whole page)? And as an addendum: I am using <code>ControlParameter</code>s pointing at the <code>SelectedValue</code> of a <code>DropDownList</code>.</p> <p>I assume this would mean:</p> <ol> <li><code>obj2</code> would either be the value that has been set earlier in the same request (i.e. <code>UpdateValue</code> has been called before) or <code>null</code> if no other value has been set yet.</li> <li>As a <code>DropDownList</code>s <code>SelectedValue</code> cannot be <code>null</code> <code>obj3</code> will never be null.</li> <li><code>OnParametersChanged</code> will always be invoked <em>the first time <code>UpdateValues</code> is called in any request</em>.</li> </ol> <p>But if <em>the same entries are loaded into the <code>DropDownList</code> during a PostBack</em> ASP.NET retains the selection in the list across the PostBack even if ViewState is disabled (I can only guess how that works but it does...).</p> <p>This comes down to the following: if ViewState is disabled and the same values are being loaded into the <code>DropDownList</code> on every request and the user had chosen entry X "long ago" and triggered a PostBack by other means than changing the selection in the DropDownList ASP.NET would fire a <code>ParametersChanged</code>-event on the <code>ParameterCollecion</code> (because of #3 in the list above) although in my understanding the parameter did not change its value.</p> <p>Am I wrong or does this mean I cannot (should not) disable ViewState if this behaviour is causing trouble?</p> <hr> <p><strong>UPDATE</strong> (not directly related to the question)</p> <p>Just to get rid of the question how the <code>DropDownList</code> preserves the selected value through a post back if viewstate is disabled: by its implementation of <code>IPostBackDataHandler</code>...</p> <p>Reflector reveals (in <code>DropDownList</code>):</p> <pre><code>protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { string[] values = postCollection.GetValues(postDataKey); this.EnsureDataBound(); if (values != null) { base.ValidateEvent(postDataKey, values[0]); int selectedIndex = this.Items.FindByValueInternal(values[0], false); if (this.SelectedIndex != selectedIndex) { base.SetPostDataSelection(selectedIndex); return true; } } return false; } </code></pre> <p>So it essentially retrieves the value of the selected item in the <code>POST</code>ed data and if it can find an item with this key in the list of items the <code>DropDownList</code> contains it sets it as selected.</p>
    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. 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