设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

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

来源:网络收集 作者:佚名 时间:2007-04-25 23:05:55

在 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);
        }

[1] [2]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: