• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net控件使用系列之——viewstate

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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


 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap