2

I'm trying to convert a game I made (WindowsFormApplication) to an ASP.NET page.

My Problem is that I have a lot "private" variables in my WindowFormApplication and those variables are important for the game. But when after I Declare all my variables (in my Page_Load), they turn null no matter what I do(click a button, refresh the page).

Is there anyway to save my variables between buttons (other than Session, because I'd have to create like 6 more sessions)

1
  • Can you post your code (a representative example)? Commented Dec 25, 2010 at 14:45

1 Answer 1

5

You will need to save your variables in the ViewState object:

ViewState["MyValue"] = 3;
...
int myValue = (int)ViewState["MyValue"];

If you already have a property then you can just use the ViewState to hold the value, such as:

private int MyValue
{
    set { ViewState["MyValue"] = value; }
    get { return (int)ViewState["MyValue"]; }
}

If your value needs to be available in the whole application (but specific to the current user), you can use Session instead of ViewState.

If you are worried about people messing with the ViewState then here are two options:

  1. ViewState encryption
  2. Storing the ViewState in the server memory
Sign up to request clarification or add additional context in comments.

3 Comments

It is worth noting that if the variables are saved in the ViewState the user can change the values and eventually cheat.
By the way, you can still store it in the Session as well. If you have 6 variables it just means storing 6 variables in 1 session, not creating 6 different sessions.
Yea thats what I meant 6 variables in 1 session ofcourse. The private int MyValue{get ...;set...;) was perfect, exacly what I needed! (I used Session Eventually)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.