优化datagrid的viewstate

进来在项目中越来越多的使用datagrid,她的确功能强大,并且易用,自带的分页也完全能够胜任小数据量的分页,但是在自己机器上测试的好好的程序,上传到服务器就...慢如乌龟....查看了一些页面代码才发现viewstate就占据了1mb。

禁用掉吗?datagrid的多数功能都是依靠viewstate的,懒人如我是在不愿把这些东西用自定义分页重写一遍...........
想起很久以前看过一片英文文档,是关于这个的,由于当时还菜的可以,根本没有理会,现下想起来,google了一下就找到不少文章--看来被这个问题困扰的不知偶一个啦.....

下面照抄:(抄自 jgtm2000blog的一篇回复.......)


这篇文章很好懂的,讲的是怎样把 DataGrid 存放在 ViewState 中的无用数据(有时候确实如此)卡掉,大家知道,一般而言DataGrid在ViewState中会存放表格中的所有数据,这样会导致ViewState非常大;好比就这个论坛而言,光是ViewState就占了65k之多,多浪费啊。
  如果只是简单的将DataGrid的EnableViewState设置为false,那么许多事件,如排序、换页等都不会触发,那么岂不是得不偿失?其实在DataGrid中,有一个用来呈现数据的子控件:System.web.ui.webcontrol.DataGridTable,很遗憾,我在MSDN中没找到它的相关说明,但是我在跟踪DataGrid时,发现它的第一个子控件就是上述的Class(当然这是我在看到这片E文后才知道的,嘿嘿)。大家可以在SaveViewState中将生成的object的Type全部看看,再加上DataGrid.ViewState中的所有值,都不会发现DataGrid在ViewState中所保存的数据,那么是什么导致ViewState这么大,是什么在ViewState中存入数据源的数据呢?
  答案就在DataGrid的这个子控件(这个暗黑人物的存在还真让人的感情受不了)。所以呢,如果不需要在ViewState中存放这些数据源的数据(比如说为了加快速度,或者自定义分页),我们只需要:DataGrid.Controls[0].EnableViewState = false,就万事OK了。
原文:
Thomas Skovsende
Hi there!

The scenario is this:
I have alot of datagrids with data in them. This makes my ViewState rather
large(upto 1MB), so I have tried to find ways to fix this.

I first tried storing my ViewState in a session instead by overriding
SavePageStateToPersistenceMedium/LoadPageStateToPersistenceMedium. This
works fine - except when people start to open more than one
window(viewstates starts to overwrite eachother), which will result in an
invalid cast.

Then i got the idea to make a new DataGrid, inherited from the original
DataGrid - and then override the LoadViewState/SaveViewState methods.(And
fetch the data from the db on every request)

But while doing this, i discovered that those methods only are responsible
for saving ViewState for themselves - NOT their children(The DataGridTable
in the DataGrid) - this is done by an undocumented method in Control which
is defined as:
internal object SaveViewStateRecursive()

The problem here is that it is NOT virtual - so i cant override it and stop
the framework from storing the ViewState of the actual data in the datagrid!

You might argue that i should disable the saving of viewstate and code
everything manually - well - tbh, i find it silly that i have to make alot
of extra code(and ny that introduce possible bugs), when all it would take
to solve my problem is to make SaveViewStateRecursive() virtual so i can
override it. If that happened, I would just override it for my custom
datagrid - viewstate wouldnt be saved for my grid, and no bugs would be
introduced(less likely anyway!)

I might be missing some obvious solution to my problem, but if not I would
love an argument for SaveViewStateRecursive() NOT to be virtual. I am aware
that there are a slight performance overhead, but I would say that this is
so little that it doesnt matter much!

Best regards,
Thomas Skovsende


Thomas Skovsende
*SIGH*

Sometimes i just hate being a developer - after battling with this problem
for a few good hours, the obvious solution appears:

protected override object SaveViewState()
{
Controls[0].EnableViewState = false; // Disable viewstate for the
DataGridTable - hardcoded - but works for now!
return base.SaveViewState();
}

Ahh well.. another case of "stupid developer"... :o)

Best regards,
Thomas Skovsende
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值