textbox web控件 根本无视enableviewsate 是否true 或者false, 依然能够相应用户的输入
在ASP.Net中WebForm控件有个EnableViewState属性。这个属性究竟有什么用。在VS的hint: Whether the control automatically saves its state for use in round-trips. 原理就是ASP.NET引用了viewstate的机制。在服务器端保存了网页各个控件及页面的状态,这其中包括各个控件在页面上的布局,和他们各自的属性。这些值就保存在ViewState下。我们可以观察Aspx页面的html源代码,假设这个页面上有一个button按钮,和一个listBox控件,html文件如下:
<input type="hidden" name="__VIEWSTATE" value="dDwzODYzNDM5NTU7Oz7FvviJbq45bDa7QJaumIiOhZ8mOQ==" />
<input type="submit" name="Button1" value="Button" id="Button1" style="height:40px;width:96px;Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 240px" />
<select name="ListBox1" size="4" id="ListBox1" style="width:152px;Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 120px"></select>
其值是一长串字符。类型为“hidden”。这个值记录的就是各个控件和页面的状态信息。当用户对页面进行相关操作的时候,状态值发生改变,并将改变的值传递给服务器端。服务器端在比较改变后的状态值和初始值之间的区别,以响应具体的请求。
一旦页面的控件很多,这种频繁的传递控件状态值对网络的消耗是很大的,因此,ASP.Net提供了EnableViewState属性,系统默认的值为true。当设置为true时,在传递状态值时就包括该控件;如果设置为false,则传递状态值时则不包括它。既然状态值不包括该控件,则客户端对它进行的操作,服务器端是不响应的。
我们可以做个实验,在Button1_Click事件中,编写代码:
ListBox.Items.Add(”客户端点击按钮一次!”);
此时运行该应用程序,单击网页上的按钮,在ListBox中会添加内容,不断地单击,内容则不断添加。如果我们将ListBox的EnableViewState属性改为false时,不断单击按钮,则只能添加一次。
这样有什么好处呢?如果我们在开发Web应用程序时,某些控件是不需要接受用户的操作或只需要接受一次操作的时候,我们可以将这些控件的EnableViewState属性改为false,这样可以优化我们的程序,提高网络访问的速度.
按照道理这个原理对于textbox也是同样的,但是在实验过程中发现textbox 根本就无视enableviewsate 是否true 或者false.Textbox2依然能够相应用户的输入. 源码:aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 100; LEFT: 280px; POSITION: absolute; TOP: 120px"
runat="server" Text="Build.Com" EnableViewState="False">Build.Com</asp:TextBox>
<asp:TextBox id=TextBox3 style="Z-INDEX: 105; LEFT: 280px; POSITION: absolute; TOP: 200px" runat="server" EnableViewState="False" Text=""></asp:TextBox>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 168px"
runat="server" Text="" EnableViewState="False"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 360px; POSITION: absolute; TOP: 240px" runat="server"
Text="Button" EnableViewState="False"></asp:Button>
<asp:ListBox id="ListBox1" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute; TOP: 184px"
runat="server" EnableViewState="False"></asp:ListBox>
</form>
</body>
</HTML>
CS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.ListBox ListBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
//base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
ListBox1.Items.Add("客户端点击按钮一次!");
TextBox3.Text = TextBox2.Text + TextBox1.Text;
}
}
}
浙公网安备 33010602011771号