开发者

Saving variables in ASP.NET page

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:

  1. ViewState encryption
  2. Storing the ViewState in the server memory
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜