对ViewState 的解析
2008-10-01 10:52
246 查看
最近做项目,使用GridView进行数据编辑,怎么也获取不到用户输入的值,只能获取到目标TextBox和它之前绑定的值。
百思不得其解,后来在网上求助,发现原来是gridView的EnableViewState属性在作怪。下面,就来认识一下这个ViewState 。
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>
到用户输入的原因。
2:如果要使用 ViewState,则在 ASPX 页面中必须有一个服务器端窗体标记 (<form runat=server>)。
3:ViewState不使用服务器资源、不会超时,并且适用于任何浏览器,需要充分利用它。
以上信息来源于网络,不动之处,请查看原文:ViewState: All You Wanted to Know 和[b]ViewState初探 。[/b]
水平有限,不到之处,请多多包涵!
百思不得其解,后来在网上求助,发现原来是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]
水平有限,不到之处,请多多包涵!
相关文章推荐
- ViewState机制的解析(转自csdn)
- ASP.Net中的ViewState解析和压缩以及页面性能优化
- ViewPager源码解析之FragmentPagerAdapter和FragmentStatePagerAdapter
- 通过Newtonsoft.Json把类放在ViewState里再解析
- ASP.NET : ViewState解析
- ASP.Net中的ViewState解析和压缩以及页面性能优化
- ASP.NET ViewState 解析与实例
- Asp.Net底层解析(二)——视图状态ViewState与控件状态ControlState详解
- __doPostBack方法解析 __VIEWSTATE __EVENTTARGET __doPostBack __EVENTARGUMENT
- ASP.NET : ViewState解析
- ViewState基本原理完全解析
- Android RecyclerView 使用完全解析 体验艺术般的控件
- session,application,cookie,viewstate,Querystring等的作用域
- 自定义View框架完全解析
- 【Android开源项目解析】RecyclerView侧滑删除粒子效果实现——初探Android开源粒子库 Leonids
- UITableView简单解析
- SpringMVC视图解析没按照ModelAndView的viewname跳转
- 源码解析View事件的分发机制
- ASP.NET的Application、Session、Cookie、ViewState和Cache等变量的区别是什么?
- Android View绘制流程与源码解析