浅谈ViewState

参见《Pro ASP.NET 3.5 in C# 2008

 

Once your page code(.aspx) has finished running(and just before the final HTML is rendered and sent to the client), ASP.NET examines all the properties(.aspx) of all the controls on your page. If any of these properties has been changed from its initial state, ASP.NET makes a note of this information in a name/value collection. Finally, ASP.NET takes all the information it has amassed and then serializes it as a Base64 string. (A Base64 string ensures that there aren’t any special characters that wouldn’t be valid HTML.) The final string is inserted in the <form> section of the page as a new hidden field.

The next time the page is posted back, ASP.NET follows these steps:

1.       ASP.NET re-creates the page and control objects based on its default(as defined in the .aspx file). Thus, the page has the same state that it  had when it was first requested.

2.       Next, ASP.NET deserializes the view state information and updates all the controls. This returns the page to the state it was in before it was sent to the client the last time.

3.       Finally, ASP.NET adjusts the page according to the posted back form data. For example, if the client has entered new text in a text box or made a new selection in a list box, that information will be in the Form collection and ASP.NET will use it to tweak the corresponding controls. After this step, the page reflects the current state as it appears to the user.

4.       Now your event-handling code can get involved. ASP.NET triggers the appropriate events, and your code can react to change the page, move to a new page, or perform a completely different operation.

 

Using view state is a great solution because resources can be freed after each request, thereby allowing for scalability to support hundreds or thousands of requests without bogging the server down. However, it still comes with a price. Because view state is stored in the page, it results in a larger total page size. This affects the client doubly, because the client not only needs to receive a larger page, but the client also needs to send the hidden view state data back to the server with the next postback. Thus, it takes longer both to receive and post the page. For simple pages, this overhead is minimal, but if you configure complex, data-heavy controls such as the GridView, the view state information can grow to a size where it starts to exert a toll. In these cases, you can disable view state for a control by setting its EnableViewState property to false. However, in this case you need to reinitialize the control with each postback.

 

 
 

 

下面来看一段代码,在下面的代码中,我们将演示对一个控件禁用ViewState会是什么样的情况。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<body>

<form id="form1" runat="server">  

<asp:Button ID="Button1" runat="server" EnableViewState="false"

    onclick="Button1_Click" Text="Button" />   

</form>

 </body>

</html>

 

public partial class _Default : System.Web.UI.Page

    {

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                this.Button1.Text = "hello";

            }

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

        }

}

 

在以上的页面中,aspx定义Button的初始Text”Button”,但是在initialization code中,我们将ButtonText更改为”hello”.所以我们第一次打开该页面时,看到ButtonText”hello”,如果我们不禁用ViewState,点击Button,它的Text依然为”hello”;如果禁用ViewState,点击Button后,它的Text变为”Button”.具体解释如下所示:

 

1.  不禁用ViewState

a.       First Request

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Run your initialization code – Button.Text = “hello”;

                                                               iii.      Button.Text is serialized in the ViewState;

                                                               iv.      Render HTML, so a user sees “hello” on the Button.

b.      Postback Request (Click on the button)

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Deserialize and apply the view state – Button.Text = “hello”.

                                                               iii.      Run your initialization code – Button.Text = “hello”;

                                                               iv.      Run your event-handling code – Button.Text = “hello”;

                                                                v.      Serialize viewstate and render HTML.

 

2.       禁用ViewState

a.       First Request

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Run your initialization code – Button.Text = “hello”;

                                                               iii.      Button.Text is NOT serialized in the ViewState;

                                                               iv.      Render HTML, so a user sees “hello” on the Button.

b.      Postback Request (Click on the button)

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Deserialize and apply the view state – Button.Text = “Button”.

                                                               iii.      Run your initialization code – Button.Text = “Button”;

                                                               iv.      Run your event-handling code – Button.Text = “Button”;

                                                                v.      Serialize viewstate and render HTML.

内容概要:本文系统阐述了Java Persistence API(JPA)的核心概念、技术架构、核心组件及实践应用,重点介绍了JPA作为Java官方定义的对象关系映射(ORM)规范,如何通过实体类、EntityManager、JPQL和persistence.xml配置文件实现Java对象与数据库表之间的映射与操作。文章详细说明了JPA解决的传统JDBC开发痛点,如代码冗余、对象映射繁琐、跨数据库兼容性差等问题,并解析了JPA与Hibernate、EclipseLink等实现框架的关系。同时提供了基于Hibernate和MySQL的完整实践案例,涵盖Maven依赖配置、实体类定义、CRUD操作实现等关键步骤,并列举了常用JPA注解及其用途。最后总结了JPA的标准化优势、开发效率提升能力及在Spring生态中的延伸应用。 适合人群:具备一定Java基础,熟悉基本数据库操作,工作1-3年的后端开发人员或正在学习ORM技术的中级开发者。 使用场景及目标:①理解JPA作为ORM规范的核心原理与组件协作机制;②掌握基于JPA+Hibernate进行数据库操作的开发流程;③为技术选型、团队培训或向Spring Data JPA过渡提供理论与实践基础。 阅读建议:此资源以理论结合实践的方式讲解JPA,建议读者在学习过程中同步搭建环境,动手实现文中示例代码,重点关注EntityManager的使用、JPQL语法特点以及注解配置规则,从而深入理解JPA的设计思想与工程价值。
先看效果: https://pan.quark.cn/s/d787a05b82eb 西门子SCALANCE X系列交换机是西门子公司所提供的工业以太网交换机产品系列,其在工业自动化领域具有广泛的应用。 如果在应用期间遭遇固件升级失误或采用了不相容的固件版本,可能会导致交换机无法正常启动。 在这种情况下,通常能够借助FTP(文件传输协议)来恢复交换机的固件,从而使其恢复正常运作。 本文件详细阐述了利用FTP修复SCALANCE X系列交换机固件的方法,并具体说明了实施步骤。 当SCALANCE X系列交换机的固件出现故障时,设备在启动后会自动激活引导加载程序,并通过故障LED的闪烁来表明设备处于特殊情形。 在这种情形下,交换机能够充当FTP服务器,与客户端建立联系,执行固件数据的传输。 需要特别强调的是,对于SCALANCE X200系列交换机,必须经由端口1来连接FTP客户端。 在实施步骤方面,首先需要为交换机指定一个IP地址。 这一步骤通常借助西门子公司提供的PST(Product Support Tools)软件来实施。 在成功配置IP地址之后,就可以通过FTP协议与交换机内部的FTP服务器建立连接,并借助FTP客户端将固件文件传输到交换机。 需要留意的是,在传输固件文件之前,应当先从西门子技术支持网站获取对应订货号的固件版本文件。 一旦固件文件备妥,就可以开始FTP操作。 这通常涉及打开操作系统的DOS窗口,运用FTP指令连接到交换机的FTP服务器,并输入正确的用户名和密码进行身份验证。 在本案例中,用户名和密码均为“siemens”,并且传输模式设定为二进制。 随后,使用FTP的“put”指令将本地固件文件上传至交换机。 值得留意的是,固件文件名必须严格遵循大小写规则。 上传成功后,...
源码地址: https://pan.quark.cn/s/f24fc84966ae 人机交互在电子工程领域中占据着核心地位,它具体指的是单片机系统与用户之间进行信息交换和管理操作的方法。 在此过程中,单片机系统负责接收用户的输入信号,对收集到的信息进行加工处理,并通过特定媒介将处理结果呈现给用户,这些媒介包括但不限于显示器、LED指示灯以及蜂鸣器等设备。 在本探讨的主题中,我们将重点研究按键与1602液晶显示屏之间的交互机制。 1602液晶显示屏是单片机应用领域中一种极为常见的人机交互界面设备,其功能在于能够显示两行文本,每行包含16个字符。 此类显示器通常采用串行或并行接口与单片机设备进行连接,主要用途是展示程序运行的状态信息、数据读取的最终结果以及其他相关的重要资讯。 我们需要深入理解如何对1602液晶显示屏进行配置和控制。 这一过程通常涉及到初始化序列的执行,其中包括设定显示模式(例如开启/关闭状态、光标移动的方向以及是否启用闪烁效果),同时选择合适的数据传输方式(4线或8线模式)。 单片机系统必须向液晶显示屏发送特定的指令集,以此来设定上述参数。 举例来说,可以通过RS(寄存器选择)、RW(读写信号)以及E(使能)引脚与LCD设备进行通信。 接下来,我们将详细讨论按键接口的设计方案。 按键通常作为输入设备存在,允许用户向单片机系统发送指令或数据。 在单片机系统中,按键通常与IO端口相连接,通过检测IO端口电平的变化来判断按键是否被触发。 对于基础的按键应用场景,可能仅需检测按键闭合时产生的低电平信号;而对于更为复杂的应用场景,则可能需要处理消抖问题,以防止因机械接触产生的瞬间抖动导致错误的读数。 在Proteus软件环境中,我们可以构建虚拟的电路模型来模拟单片机系统,其中包括1...
数据集介绍:垃圾分类检测数据集 一、基础信息 数据集名称:垃圾分类检测数据集 图片数量: 训练集:2,817张图片 验证集:621张图片 测试集:317张图片 总计:3,755张图片 分类类别: - 金属:常见的金属垃圾材料。 - 纸板:纸板类垃圾,如包装盒等。 - 塑料:塑料类垃圾,如瓶子、容器等。 标注格式: YOLO格式,包含边界框和类别标签,适用于目标检测任务。 数据格式:图片来源于实际场景,格式为常见图像格式(如JPEG/PNG)。 二、适用场景 智能垃圾回收系统开发: 数据集支持目标检测任务,帮助构建能够自动识别和分类垃圾材料的AI模型,用于自动化废物分类和回收系统。 环境监测与废物管理: 集成至监控系统或机器人中,实时检测垃圾并分类,提升废物处理效率和环保水平。 学术研究与教育: 支持计算机视觉与环保领域的交叉研究,用于教学、实验和论文发表。 三、数据集优势 类别覆盖全面: 包含三种常见垃圾材料类别,覆盖日常生活中主要的可回收物类型,具有实际应用价值。 标注精准可靠: 采用YOLO标注格式,边界框定位精确,类别标签准确,便于模型直接训练和使用。 数据量适中合理: 训练集、验证集和测试集分布均衡,提供足够样本用于模型学习和评估。 任务适配性强: 标注兼容主流深度学习框架(如YOLO等),可直接用于目标检测任务,支持垃圾检测相关应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值