国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ASP.NET小技巧:重寫ViewState的存儲(chǔ)目的地,以提高頁面性能

2019-11-18 17:55:41
字體:
供稿:網(wǎng)友

asp.net 中,ViewState 因?yàn)樵诳蛻舳说?HTML 里占據(jù)大量的空間,并隨著頁面的 PostBack 反復(fù)傳遞于網(wǎng)絡(luò)中,一直為人垢病。但是實(shí)際上 ViewState 可以存儲(chǔ)到數(shù)據(jù)庫、緩存等任意地方,從而避免頻繁將冗長的 base64 字符串發(fā)送到客戶端。這樣做不但可以顯著提高性能(大幅度減少了網(wǎng)絡(luò)傳輸?shù)淖止?jié)數(shù)),而且如果其中的內(nèi)容也不會(huì)被輕易解密和破解。因此這個(gè)方法是很有用處的。
以下寫了一個(gè)簡單的例子,用緩存來作為 ViewState 存儲(chǔ)目的地。至于緩存的 Key,文中給出的只是一個(gè)簡單的寫法,具體可以根據(jù)情況給出嚴(yán)密的方案。

代碼大致演示如下:

<%@ 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="  </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 窗體設(shè)計(jì)器生成的代碼

        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);
                string vsRaw = Convert.ToBase64String(buffer);
                LosFormatter formatter = new LosFormatter();
                return formatter.Deserialize(vsRaw);
            }
            return null;
        }

        private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            Bind();
        }
    }
}
對(duì)于實(shí)際的應(yīng)用,如果要決定在整個(gè)程序中應(yīng)用此方案,則使用一個(gè)通用的頁面基類,在其中實(shí)現(xiàn)此機(jī)制比較合適。

出處:木野狐 BLOG


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平定县| 湖北省| 福海县| 广水市| 建阳市| 蚌埠市| 梅州市| 兴山县| 莱西市| 安乡县| 乳山市| 公主岭市| 博罗县| 郯城县| 友谊县| 五峰| 巴南区| 五河县| 石阡县| 嘉义市| 古交市| 广饶县| 临泉县| 澄迈县| 桃园县| 泾阳县| 息烽县| 青神县| 富顺县| 临澧县| 福建省| 韩城市| 郯城县| 历史| 永宁县| 丰顺县| 徐闻县| 措勤县| 和政县| 积石山| 墨竹工卡县|