ViewState.IsItemDirty("abc") 不管是不是改了这个里面的内容,为什么总是返回True????\// Implement the SaveViewState method. If the StateBag
// that stores the MyItem class's view state contains
// a value for the message property and if the value
// has changed since the TrackViewState method was last
// called, all view state for this class is deleted,
// using the StateBag.Clear method,and the new value is added.
object IStateManager.SaveViewState()
{
// Check whether the message property exists in
// the ViewState property, and if it does, check
// whether it has changed since the most recent
// TrackViewState method call.
if (!((IDictionary)_viewstate).Contains("message") || _viewstate.IsItemDirty("message"))
{
_viewstate.Clear();
// Add the _message property to the StateBag.
_viewstate.Add("message", _message);
}
return ((IStateManager)_viewstate).SaveViewState();
}
有什么办法可以让ViewState.IsItemDirty("abc")返回False?
// that stores the MyItem class's view state contains
// a value for the message property and if the value
// has changed since the TrackViewState method was last
// called, all view state for this class is deleted,
// using the StateBag.Clear method,and the new value is added.
object IStateManager.SaveViewState()
{
// Check whether the message property exists in
// the ViewState property, and if it does, check
// whether it has changed since the most recent
// TrackViewState method call.
if (!((IDictionary)_viewstate).Contains("message") || _viewstate.IsItemDirty("message"))
{
_viewstate.Clear();
// Add the _message property to the StateBag.
_viewstate.Add("message", _message);
}
return ((IStateManager)_viewstate).SaveViewState();
}
有什么办法可以让ViewState.IsItemDirty("abc")返回False?
protected override void OnInit(EventArgs e)
(
bool ret;
ret = ViewState.IsItemDirty("item"); //returns false;
ViewSTate["item"] = "1";
ret = ViewState.IsItemDirty("item"); //returns false; base.OnInit(e);
}
{
if (!IsPostBack)
{
ViewState.Add("abc", "HelloWorld");
}
Response.Write("ViewState.IsItemDirty('abc'):" + ViewState.IsItemDirty("abc") + "<hr />");
}
protected void Button1_Click(object sender, EventArgs e)
{
} protected override void OnInit(EventArgs e)
{
bool ret;
if (!IsPostBack)
{
ViewState["item"] = "1";
}
ret = ViewState.IsItemDirty("item");
Response.Write("ViewState.IsItemDirty('Item'):" + ret + "<hr />");
base.OnInit(e);
}
ViewState.IsItemDirty('Item'):False
--------------------------------------------------------------------------------
ViewState.IsItemDirty('abc'):True
更离谱了。在Init时去操作ViewState有意义吗?这个时候asp.net还没有将上一个页面的ViewState恢复回来,也就是说这时你根本没有办法用ViewState做点根据变动的状态而控制流程的程序,这个时候写几行自己玩(而不是针对实际需求来干正经事)的程序没有什么意义了。
而在OnInit里面总是返回False
我的理解是ViewState是两次请求间的保存的数据,我把数据的更改都放在了!IsPostBack里面,当我连续点击Button按钮,第二次点击button按钮时,返回来的数据与第一次点击Button时返回的内容是一样的,为什么还会返回True呢?还有就是我理解IsItemDirty的作用是如果ViewState里面的键所指的内容发生变化,会返回False..在page_load里面ViewSt……