Adding Code
The spinner control will cycle through four colors based on the click events of the two buttons on the form. To keep track of the current state of the color a mechanism needs to be put in place to keep track of the variable value through post backs to the server.
If you have not figured this out yet, you should know that the web is stateless and so are ASP.NET pages. They are instantiated, executed, rendered, and disposed on every round trip to the server. You can add statefulness using techniques like storing state on the server in Session state or by posting a page back to itself.
Before ASP.NET, restoring the values back into the form fields across multiple postbacks was entirely the responsibility of the page developer who had to pick them out one-by-one, from the HTTP form and push them back into the fields. Happily, ASP.NET does this trick automatically eliminating a lot of grunt work and code for forms.
ViewState
ViewState is the mechanism ASP.NET uses to keep track of server control state values that don't otherwise post back as part of the HTTP form. For example, the text shown by a Label control is saved in ViewState by default. As a developer, you can bind data or programmatically set the Label just once when the page first loads, and on subsequent postbacks, the label text will be repopulated automatically from ViewState. So in addition to less grunt work and less code, the benefit of ViewState is often fewer trips to the database.
ViewState is a great way to track the state of a control across postbacks since it doesn't use server resources, doesn't time out, and works with any browser. If you are a control author, you'll definitely want to check out Maintaining State in a Control.
Page authors can also benefit from ViewState in much the same way. Occasionally your pages will contain UI state values that aren't stored by a control. You can track values in ViewState using programming syntax that is similar to that for Session and Cache:
// save in ViewState
ViewState["SortOrder"] = "DESC";
// read from ViewState
string sortOrder = (string)ViewState["SortOrder"];
This technique will be employed to keep track of the value of variables within the spinner control.
Back to the Code
The spinner control will have an int variable that will keep track of the color index. Appropriately it will be called currentColorIndex. The control will also need an array of colors to cycle through. In this example 4 colors will be used but this could certainly be expanded to do more.
Page_Load Event