您的位置:首页 > 其它

对ViewState 的解析

2008-10-01 10:52 246 查看
最近做项目,使用GridView进行数据编辑,怎么也获取不到用户输入的值,只能获取到目标TextBox和它之前绑定的值。

百思不得其解,后来在网上求助,发现原来是gridView的EnableViewState属性在作怪。下面,就来认识一下这个ViewState 。

什么是ViewState?

ViewState 用来跟踪和保存控件的状态信息。否则这些信息可能会丢失,原因可能是这些值不随着 form 回发,或者根本就不在 page 的 html 中。

ViewState 中保存着代码中改变的控件属性,通过代码绑定到控件的任何数据,以及由用户操作触发,回发的任何更改。

ViewState 还提供了一个状态包(StateBag), 这是一个特殊的集合或字典(collection or dictionary), 可以用来保存,通过一个 key 来恢复任意的对象或者值。

看看MSDN怎么说:

Code

<?xml version="1.0" standalone="yes"?>

<NewDataSet>

<Table>

<pub_id>0736</pub_id>

<pub_name>New Moon Books</pub_name>

<city>Boston</city>

<state>MA</state>

<country>USA</country>

</Table>

<Table>

<pub_id>0877</pub_id>

<pub_name>Binnet & Hardley</pub_name>

<city>Washington</city>

<state>DC</state>

<country>USA</country>

</Table>

<Table>

<pub_id>1389</pub_id>

<pub_name>Algodata Infosystems</pub_name>

<city>Berkeley</city>

<state>CA</state>

<country>USA</country>

</Table>

<Table>

<pub_id>1622</pub_id>

<pub_name>Five Lakes Publishing</pub_name>

<city>Chicago</city>

<state>IL</state>

<country>USA</country>

</Table>

<Table>

<pub_id>1756</pub_id>

<pub_name>Ramona Publishers</pub_name>

<city>Dallas</city>

<state>TX</state>

<country>USA</country>

</Table>

<Table>

<pub_id>9901</pub_id>

<pub_name>GGG&G</pub_name>

<city>Muenchen</city>

<country>Germany</country>

</Table>

<Table>

<pub_id>9952</pub_id>

<pub_name>Scootney Books</pub_name>

<city>New York</city>

<state>NY</state>

<country>USA</country>

</Table>

<Table>

<pub_id>9999</pub_id>

<pub_name>Lucerne Publishing</pub_name>

<city>Paris</city>

<country>France</country>

</Table>

</NewDataSet>

总结:

1:ViewState是一种机制,ASP.NET 使用这种机制来跟踪服务器控件状态值,否则这些值将不作为 HTTP 窗体的一部分而回传。这也就是为什么我没有获取

到用户输入的原因。

2:如果要使用 ViewState,则在 ASPX 页面中必须有一个服务器端窗体标记 (<form runat=server>)。

3:ViewState不使用服务器资源、不会超时,并且适用于任何浏览器,需要充分利用它。

以上信息来源于网络,不动之处,请查看原文:ViewState: All You Wanted to Know[b]ViewState初探 。[/b]

水平有限,不到之处,请多多包涵!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: