I’m building a simple game in an ASP.NET/VB.NET web app. The game has a UI made up of several ImageButtons.
The web page’s code behind file holds an instance to the game object which will manage each turn taken by players.
Everything worked when the Game object’s methods were Shared.
The problem occurred after refactoring to make the game object work as an instance instead of a shared class. Now, when the action returns to the code behind, the instance of the game object is nothing.
I suspect that this has something to do with view state, but uh… Google hasn’t helped.
The code bits:
Public Class _Default
Inherits System.Web.UI.Page
Private _gamePanel As Panel
Private _updatePanel as UpdatePanel
Private _game as Game
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'create a new instance of the game object on first loading page
_game = New Game(width, height, cellsToWin)
End If
' DisplayGameBoard() does the following:
' * Add images to the GameBoard panel inside of the GameBoardUpdatePanel
' * Attach click event handler to each image (addressOf located in this
' code behind file
' * DisplayGameBoard() works fine the first time but fails on
' subsequent post backs because there is no game object instance
Me.DisplayGameBoard()
End Sub
(From the page directive)
Language="vb"
AutoEventWireup="false"
CodeBehind="Default.aspx.vb"
Inherits="Game._Default"
ValidateRequest="false"
EnableEventValidation="false"
EnableViewState="true"
(update panel on the web page)
<asp:UpdatePanel ID="GameBoardUpdatePanel"
runat="server"
UpdateMode="Conditional"
RenderMode="Block"
EnableViewState="true"
ViewStateMode="Enabled"
ChildrenAsTriggers="true" >
<ContentTemplate>
<asp:Label ID="PlayerName"
runat="server"></asp:Label>
<asp:Panel ID="GameBoard"
runat="server"
cssclass="gameBoard"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
It’s not ViewState, it’s simply the life time of the
_Defaultinstance.You create an instance of the
Gameclass and store that as a member of the page, and expect that instance to survive. The problem is that the instance of the page doesn’t survive.Each request for the page will result in a new instance of the
_Defaultclass to be created, and when the response is created the instance is thrown away. The reference to the instance of theGameclass that you have stored in the page is also thrown away, and you lose any way to access it.If you want to keep the instance of the
Gameclass, you can store it in theSessioncollection, which is user specific:However, you should be carful about how much you store in the user session. Normally objects that are created in a page are short lived (i.e. milliseconds), so they have a small impact on the server resources. Anything that you store in the user session will be extremely long lived in comparison. Consider how large the
Gameobject is, and if you really need to keep the entire object, or if you can keep just the information needed to recreate it for each request.