We often use ViewState when using .NET development web applications, it is a very useful stuff. So what is the viewstate? In fact, ViewState is just an instance of the StateBag class, so we usually see just The corner of the iceberg now, now we will explore the following bigger icebergs. ******************************* *********************************************************** ********** NET Framework class library StateBag class 1. Overview the view status of the AdministaNet Server control (including page). Unable to inherit this class. The Control.ViewState property is an instance of the StateBag class. For a list of all members of this type, see Statebag members. System.Object System.Web.ui.Statebag [Definition] [C #] Public Sealed Class Statebag: IStateManager, iDictionary, iCollection, IEnumerable [Thread Security] All public static (Visual Basic is shared) Members a multi-threaded operation It is safe. But not to ensure that any instance member is thread safe. [Note] page or control view status is the cumulative attribute value or view of the page or control. This class can be accessed via the Control.ViewState property. This class is the main storage mechanism for all HTML and Web Server controls. It uses the property / value to store the string associated with the control. After the OnInit method is executed for the page request, it tracks changes for these properties and saves the change to the view status of the page or control. Any phase of the control life of the control can be read from this class, but should not write to this when the control is being rendered. Such a dictionary can be added or removed from it like any dictionary object. For more information on data collection (such as a dictionary), see Combine Data into the Collection. [Example] The following example illustrates a composite Label control with the TEXT and FONTSIZE properties. These properties are saved to the view state and retrieved from the view status when the control is called on the control.
-------------------------------------------------- -------------------------------------------------- --- [C #] // This control renders values stored in view state for Text and FontSize properties.using System; using System.Web; using System.Web.UI; namespace ViewStateControlSamples {public class Label: Control {// Add property VALUES TOVIEW State with set; // Retrieve the from view state with get. public string text {get {return (string) ViewState ["text"];} set {viewstate ["text"] = value;}} public int FontSize {get {return (int) ViewState [ "FontSize"];} set {ViewState [ "FontSize"] = value;}} [System.Security.Permissions.PermissionSet (System.Security.Permissions.SecurityAction.Demand, Name = "Fulltrust")] protected override void render (HTMLTEXTWRITER OUTPUT) {Output.write ("" this.Text " font>");}}} ----------------------------- -------------------------------------------------- -------------- 2. Constructor 2.1 Statebag Construction Function Public Statebag (); New Instance of the StateBag class. This is the default constructor of this class. Public statebag (bool); Initializing a new instance of the StateBag class, the instance allows the stored status value to be uncarded. Ignore the case is true; otherwise it is false. 3. Properties 3.1 statebag.count attributes Number of StateItem objects in the StateBag object. The attribute value is the number of items in the StateBag object.
Implement ICOLLECTION.COUNT: ----------------------------------------- ---------- [C #] private string getMrulist (string selectedValue) {statebag state = viewState; if (state.count> 0) {INT UPPERBOUND = State.count; string [] keys = new string [ Upperbound]; stateItem [] Values = new stateItem [Upperbound]; state.keys.copyto (keys, 0); state.values.copyto (value, 0); stringbuilder options = new stringbuilder (); for (int i = 0 i
) ------------------------------------- -------------------------------- [C #] // add proteth value value to view state with set; // retrieve the From view state with get.public string text {get {return (string) ViewState ["text"];} set {viewstate ["text"] = value;}} ------------- -------------------------------------------------- ------------------- 3.3 StateBag.keys property Gets the key collection that represents the items in the statebag object. The attribute value is the key collection. Implementation: iDictionary.keys Note: These keys are the name of the control properties. For example, if you create a BGColor property on a custom Table control, a BGCOLOR item will be created in this collection to indicate the property of the Table control. These keys correspond to the name of the STATEITEM object stored in the current page or server control in StateBag. You can use the StateBag.GeteNumerator method to loop the collection.
[C #] The following example shows how to use the Keys property. -------------------------------------------------- ------------------- [c #] private string getMrulist (string selectedValue) {statebag state = viewstate; if (state.count> 0) {int UPPERBOUND = State.count ; string [] keys = new string [upperBound]; StateItem [] values = new StateItem [upperBound]; state.Keys.CopyTo (keys, 0); state.Values.CopyTo (values, 0); StringBuilder options = new StringBuilder (); For (int i = 0; i
-------------------------------------------------- -------------------- [C #] private string getMrulist (string seed = viewstate; if (state.count> 0) {int UPPERBOUND = State. Count; string [] keys = new string [Upperbound]; stateItem [] value value = new stateItem [UpperBound]; state.keys.copyto (keys, 0); state.values.copyto (value, 0); stringbuilder options = new Stringbuilder (); for (int i = 0; I
-------------------------------------------------- -------------------------------------------- 4.2 statebag.clear method [Overview] Remove all items from the current statebag object. [Call Form] Public Virtual Void Clear (); [Implementation] iDictionary.clear [Sample] [C #] The following example demonstrates how to use the Clear method ------------------- -------------------------------------------------- ---------------------------- [C #] // Implement the SaveViewState Method. If The Statebag // That Stores The MyItem Class's View State contains // a value for the message property and if the value // has changed since the TrackViewState method was last // called, all view state for this class is deleted, // using the StateBag.Clear method, and the new value is added.object IStateManager.SaveViewState () {// Check whether the message property exists in // the ViewState property, and if it does, check // whether it has changed since the most recent // TrackViewState method call. if ((! ((IDictionary) _ViewState) .Contains ("Message")) || (_ViewState.isitemdioty ("message"))) {{_ViewState.clear (); // add the _Message Property to the statebag. _ViewState.Add (" Message ", _Message);}} RET URN (ISTATEMANAGER) _ViewState) .saveviewState ();} ------------------------------------- -------------------------------------------------- ----------- 4.3 StateBag.GeteNumerator Method [Overview] Returns all key / value pairs of the StateItem object stored in the iterative statebag. [Call Form] Public Virtual iDictionaryEnumerator getEnumerator (); [Return Value] looped access to the number of enumerations.
[Implementation] iDictionary.GeteNumerator [example (C #)] -------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------- [C #] // Create a StateBag object to contain the view state // associated with the custom control named myControl. Use the // StateBag.GetEnumerator method to create an // IDictionaryEnumerator named myDictionaryEnumerator .ctlViewState1 ctlOne = new ctlViewState1 (); StateBag myStateBag = new StateBag (); myStateBag = ctlOne.GetState (); IDictionaryEnumerator myDictionaryEnumerator = myStateBag.GetEnumerator (); ---------------- -------------------------------------------------- ---------------------------------- 4.4 Statebag.icollection.copyTo method [Overview] This member supports .NET Framework structure, therefore does not apply to use directly from code. [Call Method] [C #] Void iCollection.copyTo (Array Array, INDEX); 4.5 Statebag.idictionary.Add Method [Overview] This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] Void iDictionary.add (Object Key, Object Value); 4.6 StateBag.IDictionary.Contains Method [Overview] This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] Bool iDictionary.Contains (Object Key); 4.7 StateBag.idictionary.remove Method [Overview] This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] void idictionary.remove (Object Key); 4.8 StateBag.ienumerable.GeteNumerator method [Overview] This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] IENUMERATOR IENUMERABLE.GETENUMERATOR (); 4.9 StateBag.isitemdioty method [Overview] Checks the StateItem object stored in the statebag, calculates whether it has been modified after the CONTROL.TRACKVIEWSTATE call. [Call Method] [C #] Public Bool ISITEMDIRTY (STRING Key); [Parameter] Key to check the key. [Note] If there is no key parameter in StateBag, this method also returns false. [Example] [C #] The following example demonstrates how to use the ISITEMDIRTY method.
-------------------------------------------------- ------------------------------------------ [C #] // Implement The SaveViewState method. If the StateBag // that stores the MyItem class's view state contains // a value for the message property and if the value // has changed since the TrackViewState method was last // called, all view state for this class is deleted, // using the StateBag.Clear method, and the new value is added.object IStateManager.SaveViewState () {// Check whether the message property exists in // the ViewState property, and if it does, check // whether it has changed Since The Most Recent // TrackViewState Method Call. if ((iDictionary) _ViewState) .Contains ("Message") || (_ViewState.isitemdioty ("message")) {{_ViewState.clear (); / / Add the _Message Property to the statebag. _ViewState.Add ("Message", _Message);}} Return ((istateManager) _ViewState) .saveViewState ();} --------------- ------------------------------------- ---------------------------------------- 4.10 statebag.istatemanager.loadviewState method This member support The .NET Framework structure, therefore does not apply to use directly from the code. [Call Method] [C #] void istateManager.LoadViewState (Object State); 4.11 Statebag.IstateManager.saveViewState method This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] Object istateManager.saveViewState (); 4.12 statebag.istateManager.trackviewState method This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] void istateManager.trackviewState (); 4.13 StateBag.Remove method [Overview] The specified key / value is removed from the StateBag object. [Call Method] [C #] Public Void Remove (String Key); [Parameter] Key To remove Item [Example] [C #] The following example shows how to use the Remove method.
-------------------------------------------------- ------------------------------------ [C #] void movepiece (string.com) {Statebag bag = ViewState; object piece = bag [fromPosition]; if (piece = null!) {bag.Remove (fromPosition); bag.Add (toPosition, piece); RenderBoard ();} else {throw new InvalidPositionException ( "There is No Game Piece At the / "from /" position. ");}} --------------------------------- -------------------------------------------------- --- 4.14 statebag.setitemdioty method This member supports the .NET Framework structure, so it does not apply to use directly from the code. [Call Method] [C #] Public Void Setitemdioty (String Key, Bool Dirty); ****************************************** *********************************************************** ************ Oh, is it a huge class? Studying here, we can see that the inside of Statebag is a STATEITEM. Now let's take a look at this huge substance. The root bone should be. ************************************************************* ********************************************************************************************************************************************************************************TION Class library STATEITEM class 1. Information [Overview] indicates that the view status information is saved in the statebag class when the view status information is continuous. Unable to inherit this class. For a list of all members of this type, see the StateItem member. System.Object System.Web.ui.StateItem [Type] [C #] Public Sealed Class StateItem [Thread Safety] All public static (Visual Basic) member is a thread safe. But not to ensure that any instance member is thread safe.
[Remarks] View status is the accumulation of page or ASP.NET Server control attribute values, and sends it to the request browser by hiding the field.
You can use the Item property or add method to explicitly add the StateItem object to the Statebag of the ASP.NET server control. Statebag then tracks changes to all of its stored items. All changes to the StateItem object are reflected in its ISDIRTY properties. These changes in the save view status phase of the server control processing, just before the control is rendered to the page, save it through the SaveViewState method. For more information, see Control Executive Life Cycle.
[Example] [C #] The following example saves a simple custom ASP.NET server control class firstcustomControl status using the Value and ISDIRTY properties of the StateItem class. After the page is sent to the server, the ISDIRTY property checks if the item is modified. This status value can be displayed by accessing the value attribute. -------------------------------------------------- -------------------------------------------------- - [C #] // Create a function that iterates through the view-state // values stored for this class and returns the // results as a string.public string EnumerateViewState () {string keyName, keyValue; string result = String. Empty; StateItem myStateItem; IDictionaryEnumerator myDictionaryEnumerator = _viewstate.GetEnumerator (); while (myDictionaryEnumerator.MoveNext ()) {keyName = (string) myDictionaryEnumerator.Key; myStateItem = (StateItem) myDictionaryEnumerator.Value; keyValue = (string) myStateItem.Value; Result = result
ViewState [" Keyname "] = " keyValue;} Return Result;} ------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ---------------------------- 2. Property 2.1 StateItem.Indirty Property [Overview] Get or set a value, this value indicates the stateItem object Whether it has been modified. [Definition] [C #] public bool isdirty {get; set;} [Property value] if the stored StateItem object is modified, TRUE; otherwise false.
2.2 StateItem.Value Attributes [Overview] Gets or sets the value of the StateItem object stored in the StateBag object. [Definition] [C #] Public Object value {get; set;} [attribute value] stores the value of StateItem in the statebag object. [Example] [C #] ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------ // Create a Function That Iterates Through the view-state // values stored for this class and returns the // results as a string.public string EnumerateViewState () {string keyName, keyValue; string result = String.Empty; StateItem myStateItem; IDictionaryEnumerator myDictionaryEnumerator = _viewstate.GetEnumerator (); while (myDictionaryEnumerator.MoveNext ()) {keyName = (string) myDictionaryEnumerator.Key; myStateItem = (StateItem) myDictionaryEnumerator.Value; keyValue = (string) myStateItem.Value; result = result "
ViewState [" keyName "] =" KeyValue;} Return Result;} --------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- *********** *********************************************************** ********************* OK, now our understanding of ViewState should be a lot, this is very good for our future programming.