ASP.Net学习笔记014--ViewState初探3

来源:互联网 发布:黑客获取数据 编辑:程序博客网 时间:2024/06/05 21:03
ASP.Net学习笔记014--ViewState初探3
为什么禁用了viewstate,还能修改label2的值
因为:viewstate只是记录label2的值,并不影响给label2进行设置
--------------------------------------------
在原来的source中添加:


宽度自增.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class 宽度自增 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack ){//直接进入的时候设置为0
            Label1.Text = "0";
            //IsPostBack这里的IsPostBack就是,以前讲的通过隐藏字段传递的
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//取到label的值,然后自增后赋值回去
        Response.Write(Label1.Text .Length );
        return;
        int i = Convert.ToInt32(Label1.Text);
            i++;
            Label1.Text = i.ToString();
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        //值自增
        int i = Convert.ToInt32(TextBox1.Text);
        i++;
        TextBox1.Text = i.ToString();
        //宽度自增
        //TextBox1 .Width.Type  = UnitType.Pixel;
        //通过上面这个代码可以看到:
        //这个宽度的单位是unit
        TextBox1.Width = new Unit(TextBox1 .Width .Value +10);
        //每次都增加十个像素
    }
//这节课添加的代码:-----------------------------------------------
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write(Label2.Text);
        //可以看到这里就不能打印出Label2.Text的值100了,而是取出了给label2赋的默认值10
        //因为表单提交的时候,viewstate被禁用,所以导致无法从viewstate中取旧值
        Label2.Text = "100";
    }
    //这节课添加的代码:-----------------------------------------------
}
----------------------------
在宽度自增.aspx中添加控件
label2,button3
<%@ Page Language="C#" EnableViewState="False" AutoEventWireup="true" CodeFile="宽度自增.aspx.cs" Inherits="宽度自增" %>


<!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>
    <style type="text/css">
        #form1
        {
            height: 70px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <div>    <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
        <asp:Label ID="Label2" runat="server" Text="10"></asp:Label>
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
    </div>


    </form>
</body>
</html>
-----------------------------------
执行效果:
点击button3之后,label2会变成100,但PEI是通过response.write打印的值
一直都是10
---------------------------------------------
 protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write(Label2.Text+"<br/>");//这个打印会打印出10
//禁用viewstate,就读不到上次给客户端的值,
        //可以看到这里就不能打印出Label2.Text的值100了,而是取出了给label2赋的默认值10
        //因为表单提交的时候,viewstate被禁用,所以导致无法从viewstate中取旧值
        Label2.Text = "100";//禁用viewstate不影响对控件赋值,写入
        Response.Write(Label2.Text + "<br/>");
//这个打印会打印出100
//禁用viewstate,在请求没有结束之前也能读出设置的值
    }
0 0
原创粉丝点击