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

推荐订阅源

B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Martin Fowler
Martin Fowler
GbyAI
GbyAI
P
Palo Alto Networks Blog
N
Netflix TechBlog - Medium
C
Cisco Blogs
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
Scott Helme
Scott Helme
TaoSecurity Blog
TaoSecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
W
WeLiveSecurity
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
月光博客
月光博客
N
News | PayPal Newsroom
Microsoft Azure Blog
Microsoft Azure Blog
G
GRAHAM CLULEY
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Know Your Adversary
Know Your Adversary
博客园 - Franky
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
V
Vulnerabilities – Threatpost
V
Visual Studio Blog
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
MyScale Blog
MyScale Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Tor Project blog

博客园 - 张谊

[转]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