PostBack后如何保留网格数据源

发布于 2024-12-14 05:44:39 字数 867 浏览 1 评论 0原文

我编写了一个自定义网格视图,我想将网格 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-21 05:44:39

您尝试放入视图状态中的任何内容都必须使用 [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?

苏别ゝ 2024-12-21 05:44:39

如果您将 gridview 数据源与数据表绑定,您可以这样做......

按如下方式声明数据表,一切都会按预期工作

    private string _theDataTable="theDataTable";
    private DataTable theDataTable
    {
            get
            {
                    if(ViewState[_theDataTable]==null)
                            return new DataTable();
                    return (DataTable)ViewState[_theDataTable];
            }
            set
            {
                    ViewState[_theDataTable] = value;
            }
    }

欢呼!

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

    private string _theDataTable="theDataTable";
    private DataTable theDataTable
    {
            get
            {
                    if(ViewState[_theDataTable]==null)
                            return new DataTable();
                    return (DataTable)ViewState[_theDataTable];
            }
            set
            {
                    ViewState[_theDataTable] = value;
            }
    }

cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文