惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
爱范儿
爱范儿
博客园 - 聂微东
美团技术团队
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
罗磊的独立博客
V
Visual Studio Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 张谊

[转]ubuntu下 手动安装 LAMP 和 JAVA环境 [转]Working with user roles and permissions in SharePoint Object Model [原]SharePoint文档库上传文档 [原]SharePoint列表与文档库EventHandeler [转]ASP.NET缓存概念及其应用浅析 window.showModalDialog以及window.open用法简介 [原]欢迎加入QQ群 [原]如何在Silverlight中使用WebService绑定DataGrid [转]Silverlight中调用远程Web Service的权限问题 [转]BeanUtils接口和类 [原]Commons- BeanUtils学习笔记 [转]Apache+Tomcat负载均衡及Session绑定的实现 [原]基于Caché多维数据库的SSH实现 [原]关于支付宝API开发的一点心得 [原]Oracle中列自增的方法 [原]Java反射示例 [原]JavaSocket实现广播聊天室 [转]翻译 一些很酷的.Net技巧 《生命如一泓清水》
[转]ViewState的使用
张谊 · 2009-09-12 · via 博客园 - 张谊

只知道有个叫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