Viewstate和逗号分隔的字符串
本文关键字:字符串 分隔 Viewstate | 更新日期: 2023-09-27 18:02:07
我读过许多关于页面如何因过度使用Viewstate而陷入困境的文章,我不确定是否使用逗号分隔的字符串,可能是3 - 4个单词并将其分割为数组
string s = 'john,23,usa';
string[] values = s.Split(',');
用于检索将会有所帮助,因为我看到我的许多同事都在这样做,可能是为了提高页面加载性能。有人能给点建议吗?
实际上,它在某些情况下确实有所不同,但似乎很棘手,而且往往无关紧要。
请看下面的例子:
示例显示了以字节为单位的ViewState
大小,这意味着没有任何内容的页面结果为68字节的ViewState
。其他都是手动加载到ViewState
的内容。
将字符串值设为0..9999 on ViewState
.
string x = string.Empty;
for (int i = 0; i < 10000; i++)
{
if (i != 0) x += ",";
x += i;
}
//x = "0,1,2,3,4,5,6,7,8...9999"
ViewState["x"] = x;
//Result = 65268 bytes
和数组:
string[] x = new string[10000];
for (int i = 0; i < 10000; i++)
{
x[i] = i.ToString();
}
ViewState["x"] = x;
//Result = also 65268 bytes
以上两种情况在可重写的SaveViewState
方法上返回时都会产生65260字节的ViewState
。比在ViewState
对象上加载少8字节
然而,在其他情况下:
//104 bytes
ViewState["x"] = "1,2,3,4,5,6,7,8,9,10"
// 108 bytes
ViewState["x"] = new string[] { "1", "2", "3" , "4", "5", "6", "7", "8", "9", "10"}
如果您重写了页面SaveViewState
方法:
protected override object SaveViewState()
{
//100 bytes
return new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
//100 bytes
return "1,2,3,4,5,6,7,8,9,10";
}
由于ViewState
是加密的,Base64 encoded,
在某些情况下可能只是字符串编码两个不同的对象,从而生成两个不同的页面输出。