views:

829

answers:

3

Hi, I write a ASPX c# page, and I MUST use a global variable like this;

public static Decimal _Total;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
          _Total=0; 
         }
    }

 public void ShowCekBanka()
    {
         _Total= 10 * 5;
    }

public void ShowNakitBanka()
{
      _Total= 10 * 10;
}

Now; I put 2 Buttons in page; Button1 is run ShowCekBanak() function;Button2 is run ShowNakitBanka() Function;

When I run first time the project I click Button1 and _Total is = 50 it is ok; BUT I open my project in another internet explorer I see My _Total value is 50 in new opened page. SO the problem is global variable _Total is too much global :) Two internet explorer pages show SAME value in _Total both are _Totals is 50; It must be like this ; First Page _Total is 50 OK, but New IE page must be _Total is 0; Is'nt it?

So How Can I fix This Problem? Thanks;

+1  A: 

A public static variable will behave like that. If what you want is to maintain the value of _Total during the postback of a page, consider the following

public decimal _Total {
   get { return (decimal) ViewState["_total"]; }
   set { ViewState["_total"] = decimal; }
}
David Hedlund
Ok I wirte like this but now; When I Click Button _Total is 0 now; each page
atromgame
Note, however, that `ViewState` is stored in the HTML page (in encoded form), so a skilled user/hacker could change this value during postback. This *might* be a security problem, depending on the exact type of your application.
Heinzi
+1 that's a valid point. Doing the exact same thing, but changing `ViewState` to `Session` is a solution to that, but depending on how you open a new window, that might come with the original issues of this item.
David Hedlund
+3  A: 

Static variables are global to the application. With asp.net there is only ever one instance of the application, which is effectively run by the web server and all user browsers share. As d. mentions there are various ways to maintain state if that's what you're after, one of which is viewstate, other options are hidden fields and the Page Context object.

Article (albeit a bit old) on asp.net state

Sohnee's answer is a far too common misconception in asp.net programming and leads to some NIGHTMARE debugging. Basically all the time you're developing your app it will work fine for you. Then when you get a reasonable amount of visitors (i.e as soon as it goes live), you'll find that customers or testers are reporting VERY WEIRD and inconsistent results. Static variables on a page are global to all instances of that page - and that means all browsers visiting that page. I've modified his example to add some sleeps into it so we can emulate what would likely happen on a live server (i.e lag).

public partial class _Default : System.Web.UI.Page 
{
    public static Decimal _Total;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            _Total = 0;
            TextBox1.Text = _Total.ToString();
        }
    }

    public void ShowCekBanka(object sender, EventArgs e)
    {
        _Total = 10 * 5;
        System.Threading.Thread.Sleep(5000);
        TextBox1.Text = _Total.ToString();
    }

    public void ShowNakitBanka(object sender, EventArgs e)
    {
        _Total = 10 * 10;
        System.Threading.Thread.Sleep(5000);
        TextBox1.Text = _Total.ToString();
    }

}

If you now open two browser windows and then hit one button in one screen and the other button in the other screen and wait a few seconds you'll see that both report the same result in their text box. Difficult to explain ... but hope that helps.

Mr Grok
A: 

If you are pressing CTRL+N to get a new window, you'll see this behaviour. If you open a brand new window from fresh, then go to the URL, you shouldn't see this.

I tested almost exactly your code and it didn't persist the value of _Total between two entirely separate browser windows. Here is your code, with a slight adjustment.

public static Decimal _Total;
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        _Total = 0;
        TextBox1.Text = _Total.ToString();
    }
}

public void ShowCekBanka(object sender, EventArgs e)
{
    _Total = 10 * 5;
    TextBox1.Text = _Total.ToString();
}

public void ShowNakitBanka(object sender, EventArgs e)
{
    _Total = 10 * 10;
    TextBox1.Text = _Total.ToString();
}

I ran the page and hit a button to get a total other than 0, then opened a new browser and the total was 0.

I then ran the page and hit a button to get a total other than 0, then pressed CTRL+N and the total was then "pre-populated".

Even so, I think your use of "static" is a "VB-ism" - try it without and I think you'll be just fine.

Sohnee