PostBack后如何保留网格数据源
我编写了一个自定义网格视图,我想将网格 DataSource
保存在 ViewState
中,但出现此异常
类型 '<>f__AnonymousType0`7[[System.Int32, mscorlib, Version=2.0.0.0, 文化=中性,PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,版本=2.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089],[System.String,mscorlib, 版本=2.0.0.0,文化=中立, PublicKeyToken = b77a5c561934e089],[System.Boolean,mscorlib, 版本=2.0.0.0,文化=中立, PublicKeyToken = b77a5c561934e089],[System.String,mscorlib, 版本=2.0.0.0,文化=中立, PublicKeyToken = b77a5c561934e089],[System.Int32,mscorlib, 版本=2.0.0.0,文化=中立, PublicKeyToken = b77a5c561934e089],[System.Int32,mscorlib, 版本=2.0.0.0,文化=中立,PublicKeyToken=b77a5c561934e089]]' 在程序集'ExtAspNet.Examples中,版本= 1.0.0.0,文化=中性, PublicKeyToken=null'未标记为可序列化。
现在,我想知道如何保留网格DataSource
?
I've written a custom grid view and I want to save grid DataSource
in ViewState
but I got this exception
Type '<>f__AnonymousType0`7[[System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
in Assembly 'ExtAspNet.Examples, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' is not marked as serializable.
Now, I want to know how can I keep the grid DataSource
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试放入视图状态中的任何内容都必须使用 [Serializable] 属性进行修饰,但由于您具有匿名类型,因此您无法执行此操作。
但除此之外,不要做你想做的事情,它会不必要地显着增加你的页面大小。如果有的话,请将数据源放入会话中并在回发时重新绑定它,但不要将其放在视图状态中。
但请注意,将大量数据放入会话中是不可扩展的,也不是一个好的做法,您必须根据数据的大小以及从后端存储获取数据所需的时间来做出决定。您是否测量过获取数据的成本有多大,例如,您可以使用缓存而不是会话吗?
Anything that you attempt to put in the viewstate must be decorated with the [Serializable] attribute, but because you have an anonymous type, you can't do it.
But besides that, don't do what you are trying to do, it will increase your page size considerably and unnecessarily. If anything, put your data source in Session and rebind it on postback but don't put it on viewstate.
Note, though, that putting a huge amount of data in session is not scalable or a good practice either, you have to base your decission depending on the size of your data and how much time it takes to get the data from the backend store. Have you measured how expensive it is to get the data, could you use Cache instead of Session, for example?
如果您将 gridview 数据源与数据表绑定,您可以这样做......
按如下方式声明数据表,一切都会按预期工作
欢呼!
if you are binding your gridview datasource with a datatable you can do like this....
Declare the datatable as follows and everything will work as expected
cheers!