14.1. Understanding State in .NET

If you are working with state management in your Web application, it is important to understand state as it works within .NET as a whole. The .NET Framework and ASP.NET provide a plethora of options in dealing with state. Some of your server-side options are described in the following table.

Session TypeDescription
ApplicationUsing the Application object, you can store state that is applicable to all users. You are unable to use this for providing different state options to different users.
CacheUsing the Cache object, you are able to also store state for every user of the application. This object supports the ability to expire the cache and it also provides the ability to set dependencies on how the cache is expired.
Database-driven SessionA means of using the Session object and having all the state stored safely on a SQL server.
SessionUsing the Session object, you are able to store state on the server on a per user basis. The Session object allows you to store the state in-proc (in the same process as the application), out-of-proc (in a different process), or even using the aforementioned database approach.

If it were all about the server-side options, it would not be that long of a story to tell. ASP.NET also includes a good list of client-side state management techniques that make the process of storing state rather easy. The following table defines your client-side options.

Session TypeDescription
ControlStateA means of providing state to controls (for the control developer) that is quite similar to ViewState.
CookieA means of storing state directly in the file system of the client. Cookies can be rejected by clients.
Hidden FieldUsing hidden fields, you can store state for a user directly in the code of the page to use on subsequent requests that are posted back to the server.
QuerystringUsing the querystring capabilities provided, you are able to store state within the actual URL on a per user basis.
ViewStateProvides the ability to use encoded state within the page code.

As you can see, there are a number of ways to work with state (not even all of them are listed). It is important to understand that there isn't a right or wrong way to work with state. It really has a lot to do with what you are trying to achieve and work with in your application. Next, this chapter will look at some of these options.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset