English 中文(简体)
global variable in ASPX C#
原标题:

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;

最佳回答

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; }
}
问题回答

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签