State Management is ASP.Net
Any web pages are HTTP based pages, so they are stateless, means they don’t know whether the requests are all coming from same client or user. And also pages are destroyed and re-created with each round-trip to the server. So information will be lost. For that reason we can using State Management.
Easily solve these problems in ASP with cookie, query string, application, and sessions. Now in ASP.NET, we still can use these functions,
Mainly there are two different ways to manage web page’s state:
1. Client-side and 2. Server-side.
Client-Side State Management
No information can be maintained on the server between round-trips. The information will be stored in the page or client machine.
Using with Cookies.
A cookie is a client-side program. cookie is a small amount of data stored either in a text file on the client's file system or in-memory in the client browser session. Cookies are mainly used for tracking data settings.
If you want to store client’s information, you can use the following code:
Response.Cookies[“myCookie’].Value=”Anil Kumar”;
Using with View State
Each control on a Web Forms page, including the page itself, has a ViewState property, it is a built-in structure for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server.
ViewState is useful to we can use it to save information between round trips to the server.
//to save information ViewState.Add(“Name”,”Anil”);
//to retrieve information string shapes=ViewState[“Name”];
Using with Query Strings
Query strings provide a simple but limited way of maintaining some state information.You can easily pass information from one page to another, In addition, the query values are exposed to the Internet via the URL so in some cases security may be an issue. A URL with query strings may look like this:
string Empid, Deptid; categoryid=Request.Params[“Empid”]; productid=Request.Params[“Deptid”];
Server-side state management: Information will be stored on the server, it has higher security but it can use more web server resources.
Using with Aplication Object
The Application object provides a mechanism for storing data that is accessible to all code running within the Web application, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.
Application.Lock(); Application[“mydata”]=”mydata”; Application.UnLock();
Using with Session object
Session object can be used for storing session-specific information that needs to be maintained between server round trips and between requests for pages. Session object is per-client basis, which means different clients generate different session object. The ideal data to store in session-state variables is short-lived, sensitive data that is specific to an individual session.
Each active ASP.NET session is identified and tracked using a 120-bit SessionID string containing URL-legal ASCII characters. SessionID values are generated using an algorithm that guarantees uniqueness so that sessions do not collide, and SessionID’s randomness makes it harder to guess the session ID of an existing session. SessionIDs are communicated across client-server requests either by an HTTP cookie or a modified URL, depending on how you set the application's configuration settings. So how to set the session setting in application configuration? Ok, let’s go further to look at it.
‘cookieless’ option can be ‘true’ or ‘false’. When it is ‘false’(default value), ASP.NET will use HTTP cookie to identify users. When it is ‘true’, ASP.NET will randomly generate a unique number and put it just right ahead of the requested file, this number is used to identify users, you can see it on the address bar of IE:
//to store information into Session Session[“myName”]=”Anil”;
//to retrieve information from the Session myName=Session[“myName”];
|
No responses found. Be the first to respond and make money from revenue sharing program.
|