Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:23:50+00:00 2026-06-14T23:23:50+00:00

I have a page that uses Ajax Page Methods. When the page first loads,

  • 0

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.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:23:51+00:00Added an answer on June 14, 2026 at 11:23 pm

    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: ASP.NET C# Static Variables are global?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page that uses AJAX to retrieve messages from a server.
I have a web page that uses jquery ajax to grab the table markup
I have an ASP.NET page that uses the ASP.NET Ajax Control Toolkit TabContainer .
I have a main page that uses ajax to load subpages and one of
The situation is that I have a page that uses some AJAX calls to
I have a page that uses the Telerik RadListView control and a Telerik RadDataPager
I have a page that uses another page via the include() function. The page
I have a page that uses jQuery with a small glitch. I managed to
I have a page that uses a background image to do rounded corners, and
I'd like to have a page that uses a child master page, fill in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.