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 Wind开发者_如何学CowFormApplication 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)
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:
- ViewState encryption
- Storing the ViewState in the server memory
精彩评论