ViewState的使用

禁用控件的ViewState,设置控件的EnableViewState="False"即可;禁用Form的ViewState:<%@ Page EnableViewState="False" %>;设置ViewState的值:ViewState( "SomeItem" ) = "Some Value";ViewState是区分大小写的,比如Response.Write( ViewState( "Count" ) )和Response.Write( ViewState( "count" ) )是不一样的。

By default, almost all ASP.NET controls retain the values of their properties between form posts. For example, if you assign text to a Label control and submit the form, when the page is rendered again, the contents of the Label control are preserved.

The magic of view state is that it does not depend on any special server or browser properties. In particular, it does not depend on cookies, session variables, or application variables. View state is implemented with a hidden form field called VIEWSTATE that is automatically created in every Web Forms Page.

Disabling View State:for example, uses the EnableViewState property to disable view state for a Label control. When the page is first loaded, a message is assigned to the Label control and the message is displayed. If you click the button, however, and reload the page, the text disappears from the label.Instead of disabling view state control by control, you also can disable view state for the whole page. You should do so when you are not taking advantage of view state and the controls in a page contain a lot of data. To disable view state for an entire page, modify the EnableViewState attribute of the Page directive.:<%@ Page EnableViewState="False" %>

Adding Values to View State :To add a value to the state bag, use a statement such as the following:ViewState( "SomeItem" ) = "Some Value"。This statement adds a new item to the state bag class named SomeItem with the value Some Value.

ViewState is case-sensitive. The following two statements are not equivalent:

 
Response.Write( ViewState( "Count" ) )

Response.Write( ViewState( "count" ) )



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值