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

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
D
DataBreaches.Net
腾讯CDC
I
InfoQ
F
Fortinet All Blogs
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
月光博客
月光博客
Recent Announcements
Recent Announcements
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.