It should look like the following in design view: There are two buttons on the page, a text box to enter string and a label to display the text stored from last session. The following example demonstrates the concept of storing session state. The above code stores only strings in the Session dictionary object, however, it can store all the primitive data types and arrays composed of primitive data types, as well as the DataSet, DataTable, HashTable, and Image objects, as well as any user-defined class that inherits from the ISerializable object. You could use the following code for the same: The session state object is a name-value pair to store and retrieve some information from the session state object. Removes all keys and values from the session-state collection.ĭeletes an item at a specified index from the session-state collection. Removes the specified item from the session state collection. Removes all the items from session state collection. The HttpSessionState class has the following properties:Īdds an item to the session state collection. The session state object is created from the HttpSessionState class, which defines a collection of session state items. The SessionID is globally unique and random. Sessions are identified and tracked with a 120-bit SessionID, which is passed from client to server and back as cookie or a modified URL. It can also keep information about the user and his preferences, and keep the track of pending operations. Session state is generally used for storing application data such as inventory, supplier list, customer record, or shopping cart. This session state object becomes part of the context and it is available through the page. When session state is turned on, a new session state object is created for each new request. When a user connects to an ASP.NET website, a new session object is created. It would produce the following result: Control StateĬontrol state cannot be modified, accessed directly, or disabled. ![]() ![]() Protected void Page_Load(object sender, EventArgs e) The code behind file for the example is shown here: A label control shows the value in the counter. Let us keep a counter, which is incremented each time the page is posted back by clicking a button on the page. The following example demonstrates the concept of storing view state. Returns a string representing the state bag object. Sets the Dirty property for the specified StateItem object in the StateBag object. Sets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it. Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.Ĭhecks a StateItem object stored in the StateBag object to evaluate whether it has been modified. Removes all the items from the collection.ĭetermines whether the specified object is equal to the current object.Īllows it to free resources and perform other cleanup operations. The StateBag class has the following properties:Īdds an item to the view state collection and existing item is updated. The state bag is a data structure containing attribute value pairs, stored as strings associated with objects. ![]() It is implemented using a view state object defined by the StateBag class which defines a collection of view state items. The entire application by setting the EnableViewState property in the section of web.config file.Ī page by setting the EnableViewState attribute of the Page directive, as Ī control by setting the Control.EnableViewState property. The view state could be enabled or disabled for: When the page is again posted back, the _VIEWSTATE field is sent to the server with the HTTP request. When a page is sent back to the client, the changes in the properties of the page and its controls are determined, and stored in the value of a hidden input field named _VIEWSTATE. It is automatically maintained across posts by the ASP.NET framework. The view state is the state of the page and all its controls. This information i.e., the current value of all the controls and variables for the current user in the current session is called the State. However, there needs to be some technique to store the information between requests and to retrieve it when required. This way, each web application can scale up to serve numerous requests simultaneously without running out of server memory. When the client disconnects from the server, the ASP.NET engine discards the page objects. Hyper Text Transfer Protocol (HTTP) is a stateless protocol.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |