Does the ASP.NET ParameterCollection (or its ParametersChanged event) depend on viewstate?
Using reflector I found the following code in the System.Web.UI.WebControls.Parameter
class:
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) && (obj2 != null)) ||
((obj3 != null) && !obj3.Equals(obj2)))
{
this.OnParameterChanged();
}
}
The line
this.OnParameterChanged();
is what triggers the ParametersChanged
-event in the end, as I understand (throug开发者_StackOverflow中文版h this._owner.CallOnParametersChanged()
in OnParameterChanged
).
Now my question is: what happens if EnableViewState
is set to false
(e.g. on the whole page)? And as an addendum: I am using ControlParameter
s pointing at the SelectedValue
of a DropDownList
.
I assume this would mean:
obj2
would either be the value that has been set earlier in the same request (i.e.UpdateValue
has been called before) ornull
if no other value has been set yet.- As a
DropDownList
sSelectedValue
cannot benull
obj3
will never be null. OnParametersChanged
will always be invoked the first timeUpdateValues
is called in any request.
But if the same entries are loaded into the DropDownList
during a PostBack 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...).
This comes down to the following: if ViewState is disabled and the same values are being loaded into the DropDownList
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 ParametersChanged
-event on the ParameterCollecion
(because of #3 in the list above) although in my understanding the parameter did not change its value.
Am I wrong or does this mean I cannot (should not) disable ViewState if this behaviour is causing trouble?
UPDATE (not directly related to the question)
Just to get rid of the question how the DropDownList
preserves the selected value through a post back if viewstate is disabled: by its implementation of IPostBackDataHandler
...
Reflector reveals (in DropDownList
):
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;
}
So it essentially retrieves the value of the selected item in the POST
ed data and if it can find an item with this key in the list of items the DropDownList
contains it sets it as selected.
You are correct: you cannot disable viewstate if you want the ASP.NET web control 'changed' events and behaviors to work.
The basic mechanism they use to determine whether there has been a change is to compare the value serialized in viewstate to the equivalent posted value (if any). TextBox.TextChanged
(for example) spells this out clearly in its class documentation:
A TextBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.
精彩评论