惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
S
Security Affairs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
A
Arctic Wolf
NISL@THU
NISL@THU
P
Proofpoint News Feed
W
WeLiveSecurity
S
Schneier on Security
AI
AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
I
Intezer
S
Securelist
Help Net Security
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
Last Week in AI
Last Week in AI
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
罗磊的独立博客
月光博客
月光博客
雷峰网
雷峰网
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

博客园 - JasonBie

使用NPOI编辑Excel C# datagridview 快速导出数据到Excel Outlook2016 不能自动配置企业Exchange的解决办法 Linq实现left join左连接 电脑端微信语音像机器人解决办法 解决sql server collation conflict Asp.net APP 重置密码的方式 jQuery dataTables 列不对齐的原因 JavaScript 获得客户端IP Entity Framework Linq 动态组合where条件 查询SQLSERVER执行过的SQL记录 Asp.net Web API 返回Json对象的两种方式 Read Excel file from C# JavaScript测试工具比较: QUnit, Jasmine, and Mocha Asp.net Form验证后造成URL参数重复的问题 MVC删除数据的方法 Session State Cookie Transferring Information Between Pages
View State
JasonBie · 2012-04-12 · via 博客园 - JasonBie

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.