这篇文章我讲介绍Asp.net服务器控件的一个令人振奋的机制----ViewState,为什么说ViewState是一个令人振奋的机制呢?我们知道,在一个web请求的持续时间之外保持信息是十分必要的。在传统asp状态管理编程中,我们可以用服务器端的session对象或者客户端的cookies对象来保持信息,但是session状态不可扩展,cookies又不能被所有的应用程序所依赖。因此,asp.net提供了所谓的视图状态,即iewstate机制,这种机制可以使页面和它的子控件在从服务器到客户端再从客户端返回的往返过程中保持状态信息,它可以在一个固有的无状态环境之上创造一个有状态的并持续执行的页面效果。
使用视图状态最简单的方法就是通过控件从Control类继承来的ViewState属性,它属于System.Web.UI.StateBag类型,是一个键/值对的字典。下面给出一个简单的实例来说明ViewState的使用,让我们对ViewState有一个直观的了解
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ServerControls
{
public class ViewStateDemoLabel : WebControl
{
private string _text;
public string Text
{
get
{
return (_text == null) ? String.Empty : _text;
}
set
{
_text = value;
}
}
public string TextInViewState
{
get
{
object o = ViewState["TextInViewState"];
return (o == null)? String.Empty : (string)o;
}
set
{
ViewState["TextInViewState"] = value;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("Text = ");
writer.Write(Text);
writer.Write("<br>");
writer.Write("TextInViewState = ");
writer.Write(TextInViewState);
}
}
}
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ServerControls
{
public class ViewStateDemoLabel : WebControl
{
private string _text;
public string Text
{
get
{
return (_text == null) ? String.Empty : _text;
}
set
{
_text = value;
}
}
public string TextInViewState
{
get
{
object o = ViewState["TextInViewState"];
return (o == null)? String.Empty : (string)o;
}
set
{
ViewState["TextInViewState"] = value;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("Text = ");
writer.Write(Text);
writer.Write("<br>");
writer.Write("TextInViewState = ");
writer.Write(TextInViewState);
}
}
}
<html>
<head>
<script runat="server">
void button1_Click(object sender, EventArgs e)
{
demolabel1.Text = textbox1.Text;
demolabel1.TextInViewState = textbox2.Text;
}
</script>
</head>
<body>
<form runat="server">
<mspuc:SiteHeader id="SiteHeader1" runat="server" SubHeading="ViewStateDemoLabel test page" Heading="Chapter 7" />
<br>
Enter your first name:
<asp:TextBox id="textbox1" runat="server" />
<br>
Enter your last name:
<asp:TextBox id="textbox2" runat="server" />
<br>
<br>
<asp:Button text="Submit" onClick="button1_Click" id="button1" Runat="server" />
<asp:Button Text="Reload" Runat="server" id="Button2" />
<br>
<br>
Here is the output from the ViewStateDemoLabel:
<br>
<msp:ViewStateDemoLabel id="demolabel1" runat="server" Font-Names="Verdana" Font-Size="Medium" />
<br>
<mspuc:SiteFooter id="SiteFooter1" runat="server" />
</form>
</body>
</html>
<head>
<script runat="server">
void button1_Click(object sender, EventArgs e)
{
demolabel1.Text = textbox1.Text;
demolabel1.TextInViewState = textbox2.Text;
}
</script>
</head>
<body>
<form runat="server">
<mspuc:SiteHeader id="SiteHeader1" runat="server" SubHeading="ViewStateDemoLabel test page" Heading="Chapter 7" />
<br>
Enter your first name:
<asp:TextBox id="textbox1" runat="server" />
<br>
Enter your last name:
<asp:TextBox id="textbox2" runat="server" />
<br>
<br>
<asp:Button text="Submit" onClick="button1_Click" id="button1" Runat="server" />
<asp:Button Text="Reload" Runat="server" id="Button2" />
<br>
<br>
Here is the output from the ViewStateDemoLabel:
<br>
<msp:ViewStateDemoLabel id="demolabel1" runat="server" Font-Names="Verdana" Font-Size="Medium" />
<br>
<mspuc:SiteFooter id="SiteFooter1" runat="server" />
</form>
</body>
</html>
这里定义了两个属性,其中Text属性没有用到ViewState而TextInViewState属性用了ViewState。运行这段代码,你发现当页面post回服务端后,当页面重新加载的时候,Text属性的值并没有被保存下来,而TextInViewState属性的值却被保存了下来,它会在上一次请求的处理过程的最后将他们的状态值存储到ViewState中。