0

When I define a variable as below:

public static int x;

The program works properly but the variable is shared by users who enter the site. So the program doesn't work when users access at same time.

If I define a variable as below:

private int x;

the program doesn't work properly. for example, Can't find where checkbox true is.

The below code is for finding where the user check changed most recently.

public static CheckBox[,] cbs = new CheckBox[14, 28];
public static bool[,] diffmap = new bool[14, 28];

or

private CheckBox[,] cbs = new CheckBox[14, 28];
private bool[,] diffmap = new bool[14, 28];



protected void CBs_CheckedChanged(object sender, EventArgs e)
{
    int cur_x = 0;
    int cur_y = 0;

    int num_reserve = 0;

    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 28; j++)
        {
            if (cbs[i, j].Checked != diffmap[i, j])
            {
                cur_x = i;
                cur_y = j;
                break;
            }
        }
    }


    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 28; j++)
        {
            diffmap[i, j] = cbs[i, j].Checked;
        }
    }

}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Peter P.
  • 67
  • 7
  • Where do you define all these variables? What are you trying to achieve? – Chetan Mar 30 '21 at 02:36
  • These variable are defined in class as member variable. I hope it find what checkbox changed properly – Peter P. Mar 30 '21 at 02:37
  • its hard to tell from your question, but because when you put it into a global variable, it seems like your problem is storing settings per user – Keith Nicholas Mar 30 '21 at 02:39
  • static is shared across the application. If you want to track the checkboxes for individual users, you should not use static. You should create member variables in the class and create spearate instances of the class for each users. – Chetan Mar 30 '21 at 02:40
  • Does this answer your question? [Static variable in asp.net page](https://stackoverflow.com/questions/5217697/static-variable-in-asp-net-page) – Lance U. Matthews Mar 30 '21 at 02:57

2 Answers2

3

Instead of private or static, keep this stuff in the Session.

Remember, in ASP.Net WebForms every time you handle any event you have a new instance of your class. The class is re-built from scratch every time the events run, and ASP.Net doesn't know anything about your private members for this process; it only knows about server controls connected to ViewState.

Therefore, you also want to minimize the number of events you handle, because rebuilding the class instance each event is expensive and slow: you have to add full round-trip latency between the user and your server on top of the time it takes your code to run before every event response, and the browser (usually) has to rebuild the entire page. Instead, think about how much of this you could do via Javascript.

Making something static does not help, because static is shared among the entire app domain, which will include multiple users.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

Please have a look at Session and state management in ASP.NET Core and specificially Session state section.

Session state

Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. (...more important information here...)

tymtam
  • 31,798
  • 8
  • 86
  • 126