这个__ViewState中的值是/wEPDwUJNTY4MDU2MDI3D2QWAgIDD2QWAgIBDxBkDxYBAgIWARAFGOiuoeeul+acuuS4juS/oeaBr+WtpumZogUY6K6h566X5py65LiO5L+h5oGv5a2m6ZmiZ2RkZMRigrkFDhTuVljiInSrJ3Mc4vQL
用Convert.FromBase64String解码出byte数组,本以为c#用utf16编码,用System.Text.Encoding.Unicode.GetString能得到正确的值,但得到的却是乱码。
如果用System.Text.Encoding.UTF8.GetBytes能得到其中的中文(计算机与信息学院),但还是很乱。我想知道asp.net是怎么从中分析出控件树的相关信息的。前台代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bumenxiugai.aspx.cs" EnableEventValidation="false"  Inherits="bumenxiugai" %><!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">
    <title>部门修改</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312" />
    <style type="text/css">
        body
        {
            font-size: 14px;
        }
        table
        {
            font-size: 14px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td align="center">
                    <table width="70%" border="1" cellspacing="0" bordercolor="#0000CC" height="40">
                        <tr>
                            <td align="center">
                                <asp:DropDownList ID="DropDownList2" runat="server">
                                    <asp:ListItem>请选择您想要删除的部门名称</asp:ListItem>
                                    <asp:ListItem></asp:ListItem>
                                </asp:DropDownList>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="Button5" runat="server" Text="Button" 
                        OnClientClick="exchange()" onclick="Button5_Click"  />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            </table>
    </div>
    </form>
</body>
</html>后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class bumenxiugai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {            if (!IsPostBack)
            {
                dataconnection dc = new dataconnection();
                DataTable dt = dc.readdata("select * from fuwucs1");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    this.DropDownList2.Items.Add(dt.Rows[i]["fuwucs"].ToString().Trim());
//此处绑定了一个项:计算机与信息学院
                }
            }
    }
     protected void Button5_Click(object sender, EventArgs e)
    {
        string viewState = Request["__VIEWSTATE"].ToString();
        byte[] byteA = Convert.FromBase64String(viewState);
        string strUTF8 = System.Text.Encoding.UTF8.GetString(byteA);
        byte[] byteB = System.Text.Encoding.UTF8.GetBytes(strUTF8);
        string strUTF16 = System.Text.Encoding.Unicode.GetString(byteB);
               byte[] byteC = System.Text.Encoding.Unicode.GetBytes(strUTF16);
        string strResult = Convert.ToBase64String(byteC);
    }
}

解决方案 »

  1.   

    string strUTF8 = System.Text.Encoding.UTF8.GetString(byteA);=>string strUTF8 = System.Text.Encoding.ASCII.GetString(byteA);
      

  2.   

    它是由 ObjectStateFormatter 序列化的,你也得用 ObjectStateFormatter 来反序列化...
      

  3.   

    用reflector看看 Page 和 HiddenFieldPageStatePersister 的源码就能明白了。
      

  4.   

    KarasCanvas说的对,用ObjectStateFormatter 类可以一步步获得pair,string等类型(包括string中的值),是树形结构,叫控件树什么的,忘了。跟ViewStateDecoder工具得到的一样。有头绪了,自己再研究研究