Asp.net使用DevExpress的某些控件不能操作ViewState的解决方案
博主现在项目中需要大量用DevExpress控件,以下简称dev,第一印象还算不错,多种主题切换,强大的数据展示功能支撑,使开发者在数据的展示方面节省了大量时间。
在表单的增删查改方面,用了ASPxCallback和ASPxCallbackPanel控件,ASPxCallback主要用于异步回发处理数据,只是不能改变UI,而ASPxCallbackPanel的强大之处补充了ASPxCallback的不足,ASPxCallbackPanel就是一个面板,我们可以看作是MS自带的UpdatePanel,ASPxCallback和ASPxCallbackPanel的功能非常强大,支持很多前端事件,在这里就不在赘述,相信使用dev的朋友们深有体会。
问题来了,使用ASPxCallback、ASPxCallbackPanel、ASPxGridView等的OnCallback后台事件时用来保存ViewState时发现当时改变了,但是页面再次回发却又恢复成了改变之前的值,为此郁闷不已,进官网找答案。
先来看提问的问题:
I am trying to save my data that populates the TreeList in a ViewState. When nodes are editted, I want to update their values in the ViewState. But no changes seem to be saved in the ViewState. The Page.SaveViewState method is not even called after an edit or after a tree is expanded (it is only called on first page load).
Why can't I update the Page ViewState from one of the AJAX callbacks from a TreeList? Do I need to do something to allow the ViewState to be changed during postbacks from the TreeList?
看来这位朋友和我的问题一样
再看有用的答案:
The ViewState is stored in a hidden field which is rendered in the page's header. During a callback, the client receives the content of the control which initiated this callback. ViewState is not sent to the client (since it sits in the header) and our controls cannot embed it in the response and then update the corresponding hidden field. A possible solution is to set the ASPxTreeList's EnableCallbacks property value to false, and use the ASPxTreeList on the MS UpdatePanel. It will be able to intercept a TreeList's postbacks and "convert" them to callbacks. It is also able to update the ViewState.
回答者说出了dev的响应原理和解决方法:将dev控件的EnableCallbacks属性设为false,然后外面套UpdatePanel即可解决,一试果然解决问题。
看来,UpdatePanel拦截了dev的postback,然后UpdatePanel自身去更新ViewState,到此,问题解决。
参考资料:http://www.devexpress.com/Support/Center/Question/Details/Q208774