我已经停用与ViewState的一个非常简单的网页比比皆是:ViewState不需要保存控制值,所以它是做什么的?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" EnableViewState="false" %>
<!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">
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" EnableViewState="false"></asp:TextBox>
<asp:DropDownList runat="server" id="mylist" EnableViewState="false">
<asp:ListItem>my item 1</asp:ListItem>
<asp:ListItem>my item 2</asp:ListItem>
<asp:ListItem>my item 3</asp:ListItem>
<asp:ListItem>my item 4</asp:ListItem>
<asp:ListItem>my item 5</asp:ListItem>
<asp:ListItem>my item 6</asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="click me"/>
</div>
</form>
</body>
</html>
代码背后
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Dropdown list value is " + mylist.SelectedValue);
}
}
正如你所看到的,没有ViewState是允许的,但它确实保留控制值,在这里看到的例如运行http://www.yart.com.au/stackoverflow/viewstate/test.aspx
编辑
latr0dectus已经在下面回答了我的问题。但是实际上需要ViewState的例子是什么?我看不到控制值以外的表单需要什么。
那条很好:D –