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

推荐订阅源

罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
博客园 - 三生石上(FineUI控件)
V
V2EX
有赞技术团队
有赞技术团队
V
Visual Studio Blog
小众软件
小众软件
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
量子位
T
Tailwind CSS Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cisco Talos Blog
Cisco Talos Blog
I
Intezer
Project Zero
Project Zero
A
Arctic Wolf
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
L
Lohrmann on Cybersecurity
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security @ Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
Help Net Security
Help Net Security
N
News | PayPal Newsroom
W
WeLiveSecurity
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog

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