ViewState(英文)是一种机制,ASP.NET 使用这种机制来跟踪服务器控件状态值,否则这些值将不作为 HTTP 窗体的一部分而回传。例如,由 Label 控件显示的文本默认情况下就保存在 ViewState 中。作为开发人员,您可以绑定数据,或在首次加载该页面时仅对 Label 编程设置一次,在后续的回传中,该标签文本将自动从 ViewState 中重新填充。因此,除了可以减少繁琐的工作和代码外,ViewState 通常还可以减少数据库的往返次数。CODE:
它是由 ASP.NET 页面框架管理的一个隐藏的窗体字段。当 ASP.NET 执行某个页面时,该页面上的 ViewState 值和所有控件将被收集并格式化成一个编码字符串,然后被分配给隐藏窗体字段的值属性(即 <input type=hidden>)。由于隐藏窗体字段是发送到客户端的页面的一部分,所以 ViewState 值被临时存储在客户端的浏览器中。如果客户端选择将该页面回传给服务器,则 ViewState 字符串也将被回传。在上面的图 2 中可以看到 ViewState 窗体字段及其回传的值。
回传后,ASP.NET 页面框架将解析 ViewState 字符串,并为该页面和各个控件填充 ViewState 属性。然后,控件再使用 ViewState 数据将自己重新恢复为以前的状态。
关于 ViewState 还有三个值得注意的小问题。
1. 如果要使用 ViewState,则在 ASPX 页面中必须有一个服务器端窗体标记 (<form runat=server>)。窗体字段是必需的,这样包含 ViewState 信息的隐藏字段才能回传给服务器。而且,该窗体还必须是服务器端的窗体,这样在服务器上执行该页面时,ASP.NET 页面框架才能添加隐藏的字段。
2. 页面本身将 20 字节左右的信息保存在 ViewState 中,用于在回传时将 PostBack 数据和 ViewState 值分发给正确的控件。因此,即使该页面或应用程序禁用了 ViewState,仍可以在 ViewState 中看到少量的剩余字节。
3. 在页面不回传的情况下,可以通过省略服务器端的 <form> 标记来去除页面中的 ViewState。
string strSql = "select id ... from T_employee";
string strSearch;
public string SortField
{
get
{
object o = ViewState["SortField"];
if (o != null)
{
return (string)o;
}
return "id";
}
set
{
ViewState["SortField"] = value;
}
}
public bool SortAscending
{
get
{
object o = ViewState["SortAscending"];
if (o != null)
{
return (bool)o;
}
return true;
}
set
{
ViewState["SortAscending"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetBind(strSql);
}
ViewState["StrSearch"] = txtsearch.Text.Trim();
strSearch = txtsearch.Text.Trim();
}
private void GetBind(string sql)
{
DataView dv = new DBAccess().GetDataSet(sql).Tables[0].DefaultView;
gridEmployee.DataSource = dv;
if (SortField != "")
{
if (!SortAscending)
{
dv.Sort = SortField + " DESC";
}
else
{
dv.Sort = SortField + " ASC";
}
}
gridEmployee.DataBind();
}
protected void gridEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridEmployee.PageIndex = e.NewPageIndex;
GetBind(strSql);
}
protected void gridEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = (LinkButton)e.Row.Cells[7].Controls[0];
btn.Attributes.Add("OnClick", "javascript:return confirm('Are you sure to delete /"" + e.Row.Cells[1].Text.Trim() + "/" record?')");
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtsearch.Text.Trim() == "")
{
GetBind(strSql);
}
else
{
string sql = "select * from T_employee where depart like '%%" + ViewState["StrSearch"] + "%%'";
DataSet ds = new DB().GetDataSet(sql);
DataView dv = new DataView(ds.Tables[0]);
if (dv.Count < 1)
{
GetBind(sql);
}
else
{
gridEmployee.DataSource = dv;
gridEmployee.DataBind();
}
}
}
protected void gridEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string uid = gridEmployee.DataKeys[e.RowIndex].Values[0].ToString();
string sql = "delete from employee where id=" + uid;
if (new DBAccess().GetDelete(sql))
{
Response.Write("<script language='javascript'>alert(successfully !')</script>");
GetBind(strSql);
}
else
{
Response.Write("<script language='javascript'>alert(unsuccessfully )</script>");
}
}
protected void gridEmployee_Sorting(object sender, GridViewSortEventArgs e)
{
SortField = e.SortExpression;
string strCurrentSotFieldName = SortField;
if (SortField.Equals(strCurrentSotFieldName))
{
this.SortAscending = !SortAscending;
}
else
{
SortAscending = true;
}
string strBranch = ViewState["StrSearch"].ToString();
string sql = "Select * from * ";
if (strBranch == "")
{
GetBind(strSql);
}
else
{
GetBind(sql);
}
}