第十节 12ViewState

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/* ViewState初控(重点,常考)
 *  用asp.net重写div文本自增 (还要同时递增label的宽度,注意width的单位是Unit类型,不是简单的int)
 *  Lable.Text = (Convert.ToInt32(Label1.Text)+1).ToString();
 *  Lable.Width = new Unit(Label1.Width.Value + 10)
 *  
 * 查看生成的源代码,ASP.Net将所有隐藏内部统一放到一个名字为_VIEWSTATE的隐藏字段中,使用序列化算法将所有隐藏内容放到一个字符串中
 * 点周几次在使用ViewStateDecoder这个工查看ViewState内容,发现了确实半角这些改变的内容放到了ViewState中
 * 
 * 禁用ViewState的方法:enableviewstate=false; 禁用ViewState以后TextBox版本不受影响
 * DIV版本受影响应,因为input的value不依靠ViewStae
 */
public partial class ViewState : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) //这个是.net自带的 
        {
            Label1.Text = "0"; //页面直接进入的时候赋值0
        }
        /*else {
            string text = Label1.Text;
            int num = Convert.ToInt32(text) + 1;
            Label1.Text = num.ToString();
        }*/
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string text = Label1.Text;
        int num = Convert.ToInt32(text) + 1;
        Label1.Text = num.ToString();
    }
}

  

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button2" runat="server" style="margin-bottom: 1px" 
            Text="为Label添加100" onclick="Button2_Click" />
        <asp:Label ID="Label1" runat="server" Text="20"></asp:Label>
    <br /><br /><br /><br />
        <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
        <asp:Button ID="Button1" runat="server" EnableViewState="False" 
            onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//回答ViewState原理的时候: 说input版本(TextBox)自增加和Div版本(Label)的不同
//Label版本的值存到了ViewState中,TextBox版本的不用存,因为TextBox不是input,自已就会提交给服务器,不需要隐藏字段了

//查看生成的源代码,asp.net将所有隐藏内容统一放到了名字为_VIEWSTATE的隐藏字段中,使用序列化算法将所有隐藏内容放到一个字符中中,
//点周几次在使用ViewStateDecoder这个工具查看ViewState内容,发现了确实将这些改变的内容提要放到了
//ViewState中,存储非表单域,非value值的容器


//禁用ViewState的方法:enableviewstate=false
//禁用ViewState以后TextBox版本不受影响,Div版本受影响,因为input的value不依靠ViewState



/* 无状态Http
 * Http协议是无状态的,不会记得上次和网页"发生了什么",如果要知道上一次的状态,一个方法是在对浏览器响应结束之前将状态信息保存到而面表单中,下次而面再向服务器发出请求的时候带上这些状态信息
 * 这样服务器就能根据这些状态信息还原上次的状态了,类似于去看病的病历本
 * 
 * 状态信息保存在隐藏字段中的缺点: 加大网站的流量,降低访问速度,机密数据放在表单中会有数据欺骗等安全问题
 *
 */

public partial class ViewStateInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string i = TextBox1.Text;
        int num = Convert.ToInt32(i) + 1;
        TextBox1.Text = num.ToString();

        //修改TextBox1的宽度
        TextBox1.Width = new Unit(TextBox1.Width.Value + 10);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("Label1的值是:"+Label1.Text+"<BR>");
        //禁用了ViewState就读不到上次给客户端的值,即使禁用了ViewState,写入到浏览器中的值也不会受影响

        Label1.Text = "100";
        //即使禁用ViewState在请求没有结束之前也能读出来设置的值
        Response.Write("Label1的值是1:" + Label1.Text + "<BR>");
    }
}

  

posted @ 2012-03-18 23:43  简单--生活  阅读(178)  评论(0编辑  收藏  举报
简单--生活(CSDN)