View State

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
            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: 
    <pages viewStateEncryptionMode="Always" /> 
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: 
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. 

posted on 2012-04-12 10:42  JasonBie  阅读(...)  评论(...编辑  收藏

My Links

Blog Stats