Ajax PageMethod accessing page-level private static property


I have a page that uses Ajax Page Methods. When the page first loads, the user is prompted to select a year. This is the only time that a PostBack occurs. The year is stored in a private static page-level integer property named SelectedYear. There are several page methods that pass data from the client to the server, but the year is always stored on the server, so that it won't have to be to be passed in again. The problem is, in a few cases, within the server WebMethod, the SelectedYear property seems to be reverting to 0. I can test for 0 and throw the error back to the client, but it would help if I could explain why it happened. At this point, I don't know. Any ideas? I'm a bit new to this style of programming. Here's a (very simplified) example of the code. The user MUST have selected a year in order to ever have reached the save function.

Here is my C# server code:

public partial class Default : System.Web.UI.Page { private static int SelectedYear; protected void YearSelected(object sender, EventArgs e) { if (sender.Equals(btnCurrentYear)) SelectedYear = 2013; else SelectedYear = 2014; } [WebMethod] public static bool Save(string FirstName, string LastName) { try { if (HttpContext.Current.User.Identity.IsAuthenticated) //Right here, SelectedYear is sometimes 0. SaveApplication(FirstName, LastName, SelectedYear); else throw new Exception("User is not logged in."); } catch (Exception ex) { throw; } } }

Here is my JavaScript client code:

function Save(FirstName, LastName) { PageMethods.Save(firstName, LastName, SaveSucceeded, SaveFailed); } function SaveSucceeded(result) { //Notify user that save succeeded. } function SaveFailed(error) { //Notify user that save failed. }


Your problem is this:

private static int SelectedYear;

You'll want to remove the static. Static means it's global and will be shared for all users/requests... so when you set it to 2013 for one user, and another user hits that page who hasn't yet selected a year, it will be set to 0... for both of them. Yikes!

Trace through your postbacks to see what is happening to that variable during your AJAX methods.

You should consider storing the value in a session variable or maybe in a hidden field on the page.

More reading on a similar post: <a href="https://stackoverflow.com/questions/1563171/asp-net-c-sharp-static-variables-are-global" rel="nofollow">ASP.NET C# Static Variables are global?</a>


  • Cannot start JBoss 7.0.2 on Windows
  • Java Swing: Improving cursor response using Threads
  • Adding Google-play-services crashes aapt.exe
  • Get Proper Progress Updates on Two Long Waited Concurrent Processes in ASP.NET
  • jQuery Ajax setTimeout JSON
  • jax-ws Undefined port type with client and server in separate projects
  • WebMethod returning generic list
  • How to make Java compiler generate line numbers in compiled code
  • How to parse Java properties which contains variables?
  • Java, will (low + high) >>> 1 overflow?
  • How to Configure Log4Net Custom Object Renderer for Generic Objects?
  • mysql select inside limit
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • How to create a new Bundle object?
  • can variables be set randomly when declaring them again?
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • Is it possible to define rest argument in OCaml?
  • C# - Most efficient way to iterate through multiple arrays/list
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • java inputstream
  • ActiveRecord query for a count of new users by day
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Android screen density dpi vs ppi
  • Get object from AWS S3 as a stream
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • How do you troubleshoot character encoding problems?
  • jquery mobile loadPage not working
  • Numpy divide by zero. Why?
  • Unanticipated behavior
  • need help with bizarre java.net.HttpURLConnection behavior
  • How to get Windows thread pool to call class member function?
  • Django query for large number of relationships
  • Converting MP3 duration time