博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
View State
阅读量:5130 次
发布时间:2019-06-13

本文共 3426 字,大约阅读时间需要 11 分钟。

A View State Example

public 
partial 
class SimpleCounter : System.Web.UI.Page 
    
protected 
void cmdIncrement_Click(Object sender, EventArgs e) 
    { 
        
int counter; 
        
if (ViewState[
"
Counter
"] == 
null
        { 
            counter = 
1
        } 
        
else 
        { 
            counter = (
int)ViewState[
"
Counter
"] + 
1
        } 
 
        ViewState[
"
Counter
"] = counter; 
        lblCount.Text = 
"
Counter: 
" + counter.ToString(); 
    } 
}
If your view  state contains some information you want to keep secret, you can enable view state encryption . 
You can turn on encryption for an individual page using the ViewStateEncryptionMode property of the Page directive: 
<%@Page ViewStateEncryptionMode=
"
Always
"  %> 
Or you can set the same attribute in a configuratio n file to configure view state encryption for all the 
pages in your website: 
<
configuration
> 
  
<
system.web
> 
    
<
pages 
viewStateEncryptionMode
="Always"
 
/> 
    ... 
  
</
system.web
> 
</
configuration
> 
You have three choices for your view state encryption   setting—always encrypt (Always),  never encrypt (Never), or encryp t only if a control specifically  requests it (Auto).

 

Retaining Member Variables

public 
partial 
class PreserveMembers : Page
{
  
//
 A member variable that will be cleared with every postback.
  
private 
string contents;
  
protected 
void Page_Load(Object sender, EventArgs e)
  {
     
if (
this.IsPostBack)
     {
        
//
 Restore variables.
        contents = (
string)ViewState[
"
contents
"];
     }
  }
  
protected 
void Page_PreRender(Object sender, EventArgs e)
  {
     
//
 Persist variables.
     ViewState[
"
contents
"] = contents;
  }
  
protected 
void cmdSave_Click(Object sender, EventArgs e)
  {
     
//
 Transfer contents of text box to member variable.
     contents = txtValue.Text;
     txtValue.Text = 
"";
  }
  
protected 
void cmdLoad_Click(Object sender, EventArgs e)
  {
     
//
 Restore contents of member variable to text box.
     txtValue.Text = contents;
  }
}

 

 

Storing Custom Objects

to store an item in view state, ASP.NET must be able to convert it into a stream of bytes so that it can be added to the hidden input fie ld in the page. This process is called  serialization . If your objects aren’t serializable (and by default they’re not), you’ll receive an error message when you attempt to place them in view state. 
To make your objects serializable, you need to add a Serializable attribute before your class declaration. For example, here’s an exceedingly simple Customer class: 
[Serializable] 
public 
class Customer 
    
private 
string firstName; 
    
public 
string FirstName 
    { 
        
get { 
return firstName; } 
        
set { firstName = value; } 
    } 
 
    
private 
string lastName; 
    
public 
string LastName 
    { 
        
get { 
return lastName; } 
        
set { lastName = value; } 
    } 
 
    
public Customer(
string firstName, 
string lastName) 
    { 
        FirstName = firstName; 
        LastName = lastName; 
    } 
Because the Customer class is marked as serializable, it can be stored in view state: 
//
 Store a customer in view state. 
Customer cust = 
new Customer(
"
Marsala
"
"
Simons
"); 
ViewState[
"
CurrentCustomer
"] = cust; 
Remember, when using custom objects, you’ll need  to cast your data when you retrieve it from view state. 
//
 Retrieve a customer from view state. 
Customer cust; 
cust = (Customer)ViewState[
"
CurrentCustomer
"];
If the class declaration is preceded  with the Serializable attribute (as it is here), instances of this class can be placed in view state. If  the Serializable attribute isn’t present, the class isn’t serializable, and you won’t be able to place instances of it in view state. 

转载于:https://www.cnblogs.com/JasonBie/archive/2012/04/12/2443516.html

你可能感兴趣的文章
linux命令之ifconfig详细解释
查看>>
NAT地址转换
查看>>
Nhibernate 过长的字符串报错 dehydration property
查看>>
Deque - leetcode 【双端队列】
查看>>
人物角色群体攻击判定(一)
查看>>
一步步学习微软InfoPath2010和SP2010--第九章节--使用SharePoint用户配置文件Web service(2)--在事件注册表单上创建表单加载规则...
查看>>
gulp插件gulp-ruby-sass和livereload插件
查看>>
免费的大数据学习资料,这一份就足够
查看>>
clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight
查看>>
MySQL(一)
查看>>
企业级应用与互联网应用的区别
查看>>
steelray project viewer
查看>>
itext jsp页面打印
查看>>
HTTP之报文
查看>>
Perl正则表达式匹配
查看>>
Git
查看>>
DB Change
查看>>
nginx --rhel6.5
查看>>
Eclipse Python插件 PyDev
查看>>
selenium+python3模拟键盘实现粘贴、复制
查看>>