[转]ViewState的使用

只知道有个叫Session的东西,可以跨页面保存变量,在它的存在时期。现在又有个叫ViewState的东西,我是没用过,现在也该开始用了,一般是用来保存控件的值或状态。
      如果你把表单用动态的table来排版,就可以来遍历控件了。
      先看页面代码:

        <table id="Table1" runat="server" style="width: 221px">
            <tr>
                <td style="width: 60px">Name</td>
                <td><asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 60px">From</td>
                <td><asp:TextBox ID="FromTextBox" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 60px">To</td>
                <td><asp:TextBox ID="ToTextBox" runat="server"></asp:TextBox></td>
            </tr>
        </table>
    有了runat ="server"就是服务器控件了,然后看怎么遍历。
    先是保存到各自的 ViewState

        foreach (HtmlTableRow row in Table1.Rows)
        {
            foreach (HtmlTableCell cell in row.Cells)
            {
                Control control = cell.Controls[0];
                if (control is TextBox)
                {
                    ViewState[control.ID] = ((TextBox)control).Text;
                }
            }
        }
    然后是读取出来再重新赋值

 foreach (HtmlTableRow row in Table1.Rows)
        {
            foreach (HtmlTableCell cell in row.Cells)
            {
                Control control = cell.Controls[0];
                if (control is TextBox)
                {
                    string value = (string)ViewState[control.ID];
                    if (value != null)
                    {
                        ((TextBox)control).Text = value;
                    }
                }
            }
        }
    一个最科学的方法是建立一个对应的类,然后用类来操作。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for TravelPlan
/// </summary>
[Serializable]
public class TravelPlan
{
    private string name, from, to;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string From
    {
        get { return from; }
        set { from = value; }
    }

    public string To
    {
        get { return to; }
        set { to = value; }
    }

 public TravelPlan(string n, string f, string t)
 {
        name = n;
        from = f;
        to = t;
 }
}
     将表单上的对象建立为类的成员,建立构造函数,然后调用就方便多了,记得加绿色部分。
    存值:

TravelPlan travelplan = new TravelPlan(NameTextBox.Text, FromTextBox.Text, ToTextBox.Text);
        ViewState["TravelPlan"] = travelplan;
    取值:

        TravelPlan travelplan = ViewState["TravelPlan"] as TravelPlan;
        if (travelplan != null)//记得判断
        {
            NameTextBox.Text = travelplan.Name;
            FromTextBox.Text = travelplan.From;
            ToTextBox.Text = travelplan.To;
        }
     ViewState在客户端展开的时候,默认是Auto,不加密的,如果页面有限制性的表单控件才加密,所以,你可以查看,代码如下:

            byte[] bytes = Convert.FromBase64String(ViewStateTextBox.Text);
            DecodedDataTextBox.Text = System.Text.Encoding.ASCII.GetString(bytes);
    要设置加密可以在2个地方,一个是页面代码顶部:

<%@ Page ViewStateEncryptionMode="Always" Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    还有一个在Web.config里的<system.web>与</system.web>里加:

< Page ViewStateEncryptionMode="Always" />
     当然,Web.config里的配置决定了整个网站页面。

本文来源于Woody的鸟窝(Woody's Blog) http://www.smartgz.com, 原文地址:http://smartgz.com/blog/Article/911.asp

posted @ 2009-09-12 21:36  张谊  阅读(431)  评论(0编辑  收藏  举报