ASP.NET: A State of Mind

While it can sometimes be enough that a webpage only need to be re-constructed in exactly one way each time a request for it is made — such as how you're viewing the webpages of this course website — there are plenty of times where you want to keep a memory of actions a user has taken. From actions on a single page to actions taken across a series of pages. To accomplish this, you can manage the state of your ASP.NET website. There are four types of state to be managed:

View State can be thought of as the "normal", public-facing view of any given webpage. When you go to visit a webpage, a request is sent to the server, which constructs the requested file, and sends it back to you to then view. This doesn't have to mean the requested file is exactly the same for every time a page is requested, but only that there is no continuity among all the requested pages.

Control State cannot be modified, accessed directly, or disabled. It is designed to store important data about containers on that webpage (such as the selected item from a DropDownBox) that may need to be available when submitted an ASP.NET page to the server for processing, called a PostBack.

Session State is the additional context added to the website experience for identifiable users. Most typically accomplished through cookies or entering authentication to log into a pre-existing account. Sessions are what allow you to accomplish goals such as logging into Amazon.com, opening up a dozen or so tabs of Christmas gifts you're considering for friends and loved ones, with each one of those pages knowing who you are and what the current state of your shopping cart is.

Application State refers to the collection of all web pages, source code, and other files within a single virtual directory on a web server. You can think of this as the "global" view of your entire web site. This can be used to store site-wide symbolic constants, or relevant statistics about the current state of the web site in general, such as how many logged in Redditors are currently view any given Subreddit content at any given time.

There are three dictionary-type collections referring to each state (except Control), called ViewState, Session, and Application, whereby you can use unique subscript strings (e.g. Application["activity_factorio"] = String.Format("There are {0} Engineers currently automating production.", Application["subreddit_factorio"].ActiveCounter);) to assign relevant values to be accessed by anyone using the website. Or something like:

greetingsLabel.Text = "Welcome " + this.Session["RognessDaniel"].LoginID + "!";