c# - Ajax TimerControl and Instance variables? -


I am trying to write an instance member of my page class while using Ajax timer control. / P>

I have created a very straightforward crude example below, which imitates the exact problem I am getting ...

The code below is called "Update Text" One method uses every other timer to control. Every time the method is said, how many times the timer controls have created the callback. For example, to read the value of the counter on the web page, the label sits with the timer-connected trigger within an updated panel.

Markup:

  & lt; Form id = "form1" runat = "server" & gt; & Lt; Div & gt; & Lt; Asp: ScriptManager id = "ScriptManager1" runat = "server" & gt; & Lt; / ASP: ScriptManager & gt; & Lt; ASP: Timer ID = "Timer 1" Runat = "Server" Interval = "1000" ontic = "Update Text" & gt; & Lt; / ASP: Timer & gt; & Lt; Asp: UpdatePanel ID = "Update Panel 1" Runat = "Server" & gt; & Lt; ContentTemplate & gt; & Lt; ASP: label runat = "server" text = "" id = "lbl counter" & gt; & Lt; / ASP: Labels & gt; & Lt; / ContentTemplate & gt; & Lt; Trigger & gt; & Lt; Asp: AsyncPostBackTrigger ControlID = "timer 1" /> & Lt; / Trigger & gt; & Lt; / ASP: UpdatePanel & gt; & Lt; / Div & gt; & Lt; / Form & gt;  

CodeBehind:

Public partial class timer control: system. Web. UI Page {Private Static Ent Loop Control = 0; Secure Zero Page_load (Object Sender, EventErgus E) {} Public Zero Update Text (Object Sender, EventAgds E) {lblCounter.Text = loopCounter.ToString (); LoopCounter ++; }}

If you run it you will be able to work it, great, but I do not want the 'loop quater' variable to be constant because it applies to the entire app level and all users One must read / write the same variable.

When I try to remove the static keyword, the loop control variable is no longer updated, it just stays at zero ... there is no error in the code or browser (javascript)

I have found almost a temporary job using sessions that work well and live in the context of a user, but I do not want to use sessions, unless I really Do not be in the

Can anyone please explain why an 'example' member can not write such a thing? Why does it work for the stable only?

itemprop = "text">

Why this happens basically because http is a stateless protocol, your problem The specific reason for this is that instances of pages are not kept in memory after the request is over. Every time you make a request on the ASP.Net page, a new page instance is created. You can easily check it by adding a default constructor to your page class and insert breakpoint.

In addition to using session status, you can also use it for this. In this case it would probably be a better solution, because the data you are using (your counter) is not user-specific but page-specific.


Comments