html代码:

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

<!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>
<script language="javascript" type="text/javascript">
// <!CDATA[

 

// ]]>
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            
        <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>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保持状态" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="写出状态" />
    
    </div>
    </form>
        <br />      
 
</body>
</html>

 

 

button1的单击事件


        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;
                }
            }
        }

 

button2的单击事件

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)
                    {
                        Response.Write("value is :" + value + "<br>");
                    }
                }
            }

===================================================================

【一种比较好的设计方法】


一个最科学的方法是建立一个对应的类,然后用类来操作


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" />


 

posted on 2009-03-28 10:38  tsliudong  阅读(172)  评论(0编辑  收藏  举报