ASP.Net学习笔记014--ViewState初探3
2017-04-24 20:19
375 查看
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,在请求没有结束之前也能读出设置的值
}
为什么禁用了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,在请求没有结束之前也能读出设置的值
}
相关文章推荐
- ASP.NET 学习笔记_03 页面跳转、调试、form表单、viewstate、cookie
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.NET MVC 学习笔记-5.Controller与View的数据传递
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.NET MVC学习笔记-ViewEngine 深入解析与应用实例
- ASP.Net学习笔记013--ViewState初探2
- ASP.Net MVC开发基础学习笔记(10):分部视图PartialView
- ASP.NET MVC学习笔记:(二)return View(...)
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.NET 学习笔记_13 viewstate 和 cache
- ASP.NET MVC学习笔记-----使用自定义的View Engine
- 【jqGrid for ASP.NET MVC Documentation】.学习笔记.2.jqGrid Model-View-Controller 分离
- ASP.NET控件学习笔记之ViewState
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.Net学习笔记012--12ViewState初探
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
- Asp.net MVC 学习笔记 (3) --Controller 与View 之间的数据传递
- ASP.Net WebForm温故知新学习笔记:二、ViewState与UpdatePanel探秘
- Asp.net core 学习笔记 ( ViewComponent 组件 )