王朝网络
分享
 
 
 

ASP.NET小技巧:重写ViewState的存储目的地,以提高页面性能

王朝asp·作者佚名  2006-11-24
宽屏版  字体: |||超大  

在 ASP.NET 中,ViewState 因为在客户端的 HTML 里占据大量的空间,并随着页面的 PostBack 反复传递于网络中,一直为人垢病。但是实际上 ViewState 可以存储到数据库、缓存等任意地方,从而避免频繁将冗长的 base64 字符串发送到客户端。这样做不但可以显著提高性能(大幅度减少了网络传输的字节数),而且如果其中的内容也不会被轻易解密和破解。因此这个方法是很有用处的。

以下写了一个简单的例子,用缓存来作为 ViewState 存储目的地。至于缓存的 Key,文中给出的只是一个简单的写法,具体可以根据情况给出严密的方案。

代码大致演示如下:

<%@ Page language="c#" Codebehind="SaveViewStateToOther.aspx.cs" AutoEventWireup="false" Inherits="LinkedList.SaveViewStateToOther" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>

<head>

<title>SaveViewStateToOther</title>

<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

<meta name="CODE_LANGUAGE" Content="C#">

<meta name=vs_defaultClientScript content="JavaScript">

<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">

</head>

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server"><asp:DataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 72px" runat="server" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" PageSize="6" AllowPaging="True">

<selecteditemstyle font-bold="True" forecolor="#CCFF99" backcolor="#009999">

</SelectedItemStyle>

<itemstyle forecolor="#003399" backcolor="White">

</ItemStyle>

<headerstyle font-bold="True" forecolor="#CCCCFF" backcolor="#003399">

</HeaderStyle>

<footerstyle forecolor="#003399" backcolor="#99CCCC">

</FooterStyle>

<pagerstyle horizontalalign="Left" forecolor="#003399" backcolor="#99CCCC" pagebuttoncount="20" mode="NumericPages">

</PagerStyle>

</asp:DataGrid>

</form>

</body>

</html>

using System;

using System.Data;

using System.IO;

using System.Text;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace LinkedList

{

/// <summary>

/// SaveViewStateToOther 的摘要说明。

/// </summary>

public class SaveViewStateToOther : Page

{

protected DataGrid DataGrid1;

private void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

Bind();

}

private void Bind()

{

DataTable table = new DataTable();

table.Columns.Add("id", typeof (int));

table.Columns.Add("name", typeof (string));

for (int i = 0; i < 1000; i++)

{

DataRow row = table.NewRow();

row["id"] = i;

row["name"] = "student_" + i.ToString();

table.Rows.Add(row);

}

DataGrid1.DataSource = table;

DataGrid1.DataBind();

}

#region Web 窗体设计器生成的代码

protected override void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

protected override void SavePageStateToPersistenceMedium(object viewState)

{

LosFormatter format = new LosFormatter();

StringWriter writer = new StringWriter();

format.Serialize(writer, viewState);

string vsRaw = writer.ToString();

byte[] buffer = Convert.FromBase64String(vsRaw);

string vsText = Encoding.ASCII.GetString(buffer);

object v = Cache[PageKey];

if (v == null)

Cache.Insert(PageKey, vsText);

else

Cache[PageKey] = vsText;

}

public string PageKey

{

get { return Session.SessionID + "_page_SaveViewStateToOther_aspx"; }

}

protected override object LoadPageStateFromPersistenceMedium()

{

object s = Cache[PageKey];

if (s != null)

{

string state = s.ToString();

byte[] buffer = Encoding.ASCII.GetBytes(state);

 

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有