查看ASP.NET : ViewState

1,Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>测试</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    显示结果:<br />
    <asp:Label ID="result" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

 

2,Default.aspx.cs

 


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

   }

         protected override void SavePageStateToPersistenceMedium(object viewState)
         {
            // 调用基类的方法,完成基本操作
            base.SavePageStateToPersistenceMedium(viewState);
            // 获取ViewState的Base64值
            LosFormatter format = new LosFormatter();
            StringWriter writer = new StringWriter();
            format.Serialize(writer, viewState);
            string vsRaw = writer.ToString();
            Response.Write("ViewState Raw: " + Server.HtmlEncode(vsRaw)); Response.Write("");
            // 解析内容
            byte[] buffer = Convert.FromBase64String(vsRaw);
            string vsText = Encoding.ASCII.GetString(buffer);
            Response.Write("ViewState Text: " + Server.HtmlEncode(vsText));
        }
}

 

你可能感兴趣的:(asp.net)