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

推荐订阅源

小众软件
小众软件
IT之家
IT之家
博客园 - 聂微东
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
美团技术团队
S
Secure Thoughts
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
T
Tailwind CSS Blog
月光博客
月光博客
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
S
Security @ Cisco Blogs
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
S
Security Affairs
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
O
OpenAI News
L
Lohrmann on Cybersecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 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.