简体   繁体   中英

Why isn't this static c# variable changing?

I have a couple of C# variables in a protected class that I am using to store the last coordinates clicked on an HTML canvas and the variables aren't changing when the method is called:

public partial class Default : Page {

    public static int xVar {get; set;}
    public static int yVar {get; set;}

    protected void Page_Load(object sender, EventArgs e) {
    }

    protected string getDate() {
        return Convert.ToString(DateTime.Now.Year); 
    }

    [WebMethod]
    protected void InsertPoints(int x, int y) {
        xVar = x;
        yVar = y;
        //SQL Connect stuff and other code that works
    }
}

The method is called from an aspx page and it does call it. The problem is it initializes the variables to zero and they remain that way regardless where I click in the Canvas. In fact when there is a SQL insertion, it inserts the correct integers (x and y), but I can't get the static variables to change. Thanks.

Alright, I'll make a guess from what I've learned in comments.

I think there's an error here in the workflow you're using to solve the problem.

What's happening, and it's really your call whether this is what you expect or not, is that you're:

  1. Grabbing the first page
  2. Clicking on your canvas, which fires a web request up to update the static properties.
  3. Expecting that alert call to be updated with the new xCoord value.

The key here is where the <%=...%> gets evaluated. The evaluation of that expression is happening when you pull the page for the first time, it never gets called again until you fire another request.

In other words, alert('<%= xCoord %>') is evaluated server-side upon the page loading, the browser is getting the final alert('0') , not some magical link up to the original static variable.

The quick check for this is to refresh the page. If you:

  1. Click the canvas,
  2. Refresh the page, then
  3. Click the canvas again,

it should be updated correctly.

The only way you'll be able to achieve the functionality that it sounds like you want is by either using the values you push up to the web method, which will only work on the originating browser window, or use a library like SignalR to handle live push-downs of the data to all connected browsers.

Let me know if I've misunderstood your setup, or if I can clarify any of this. Again, my understanding is mostly just an inference from what you've said in comments. I know this is right , but you'll need to confirm whether it's relevant.


A few people, including myself, mentioned this in comments, but I'll also say it here. From my own comment,

Be careful using static stuff in ASP. You can run into some threading problems, and multiple instances of the static fields or properties. It gets messy. Only use static when you're okay with the data potentially being outdated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM