ViewState mechanism 3

Source: Internet
Author: User

2.2.4 StateBag Class

ViewState is a property of a control that uses a control to have memory capabilities. In the preceding narration, we can see that some properties of the control can restore the original value by using ViewState, save the value of this time, and the implementation of many methods in the controls class also directly calls the ViewState method. The ViewState type is statebag, so let's take a look at how these functions are implemented in StateBag. The StateBag definition is declared in System.Web.UI as follows:

Public sealed class Statebag:istatemanager, IDictionary, ICollection, IEnumerable

The StateBag class can be understood as a dictionary with state management capabilities because it implements IStateManager, IDictionary these two interfaces. The StateBag class can save Key/value pairs like a dictionary, where key is a string and value is an object. Here is an example of using StateBag.

protected void button2_click (object sender, EventArgs e)
{
StateBag testsb = new StateBag ();
Testsb["B"] = "bbbbb";
TextBox1.Text = testsb["B"]. ToString ();
}

In the above example, use StateBag to save a key "B" with the value "BBBBB" of the key/value pair. The ViewState property is also an instance of StateBag, and it can, of course, be used as above. A number of key/value pairs (key-value pairs) are saved in the ViewState, which are key/value to hold the properties of the control, and these key/value are maintained by asp.net. Of course, we can also add some of our own key/value to save some information.

StateBag also implements the System.Web.UI.IStateManager interface so that it has state management capabilities. The following is a description of how StateBag provides state management functionality.

1) Stateitem class

StateBag, the key is a string type and value is the type object. \ key/value But saving value inside StateBag is not an object type, but instead converts the object type to a Stateitem type and then saves it, and then converts the Stateitem type to type object when it is removed from the statebag. That is to say, the key/value in StateBag is actually string/stateitem type. The conversion process is implemented within the StateBag and is not felt by the customer. Stateitem's statement reads as follows:

public sealed class Stateitem
{
internal Stateitem (object initialvalue);
public bool IsDirty {get; set;}
Public object Value {get; set;}
}

With the above code we can see that there is actually a isdirty attribute to mark whether the current value has been modified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.