Ajax TimerControl和Instance变量?(Ajax TimerControl and Instance variables?)

编程入门 行业动态 更新时间:2024-10-17 05:02:09
Ajax TimerControl和Instance变量?(Ajax TimerControl and Instance variables?)

在使用Ajax计时器控件时,我一直在尝试写入我的页面类的实例成员。

我在下面构建了一个非常简单的粗略示例,它模拟了我遇到的确切问题......

下面的代码使用定时器控件每秒执行一个名为“UpdateText”的方法。 每次调用该方法时,它都会将计数器变量递增一,以说明计时器控件进行回调的次数。

当然,为了将计数器的值读取到网页上,标签位于更新面板内,其中触发器与计时器相关联。

标记:

<form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="UpdateText"> </asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label runat="server" Text="" ID="lblCounter"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" /> </Triggers> </asp:UpdatePanel> </div> </form>

代码隐藏:

public partial class TimerControl : System.Web.UI.Page { private static int loopCounter = 0; protected void Page_Load(object sender, EventArgs e) { } public void UpdateText(object sender, EventArgs e) { lblCounter.Text = loopCounter.ToString(); loopCounter++; } }

如果你运行它你会发现它很有用,但是我不希望'loopCounter'变量是静态的,因为这将适用于整个应用程序级别,并且所有用户都将读/写同一个变量。

当我尝试删除static关键字时,loopCounter变量不再更新,它只保持为零...代码或浏览器(JavaScript)中没有给出错误。

我找到了一个临时的工作,通过使用会话,它很好地工作,并保持在用户的上下文,但我不想使用会话,除非我真的必须。

任何人都可以解释为什么'实例'成员不能这样写? 为什么它只适用于静态?

I've been having an issue trying to write to an instance member of my page class when using the Ajax timer control.

I've built a very simple crude example below which emulates the exact problem I'm having...

The code below executes a method called "UpdateText" every second using the timer control. Every time the method is called it increments a counter variable by one to illustrate how many times the timer control has made a callback.

Of course, in order to read the value of the counter onto the web page the label sits inside an update panel with a trigger associated to the timer.

Markup:

<form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="UpdateText"> </asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label runat="server" Text="" ID="lblCounter"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" /> </Triggers> </asp:UpdatePanel> </div> </form>

CodeBehind:

public partial class TimerControl : System.Web.UI.Page { private static int loopCounter = 0; protected void Page_Load(object sender, EventArgs e) { } public void UpdateText(object sender, EventArgs e) { lblCounter.Text = loopCounter.ToString(); loopCounter++; } }

If you run this you'll find it works, great but I don't want the 'loopCounter' variable to be static as this would apply to the whole application level and all users would be reading/writing to the same variable.

When I try to remove the static keyword, the loopCounter variable is no longer updated, it just stays at zero... There are no errors given in the code or in the browser (JavaScript).

I've found a temporary work around by using sessions instead which work nicely and remain in the context of a user but I prefer not to use sessions unless I really have to.

Could anyone please explain why an 'instance' member cannot be written to like this? Why does it only work for static?

最满意答案

发生这种情况的原因主要是因为http是无状态协议。 您遇到问题的具体原因是请求完成后页面实例不会保留在内存中。 每次向ASP.NET页面发出请求时,都会创建一个新的页面实例。 您可以通过向页面类添加默认构造函数并在其中放置断点来轻松地自行检查。

除了使用会话状态,您还可以使用viewstate 。 在这种情况下,这可能是一个更好的解决方案,因为您使用的数据(您的计数器)不是特定于用户的,而是特定于页面的。

The reason this happens is essentially because http is a stateless protocol. The specific reason for your problem is that instances of pages are not kept in memory after a request has finished. Each time you make a request to an ASP.NET page, a new page instance is created. You can easily check this for yourself by adding a default constructor to your page class and putting a breakpoint there.

Besides using session state, you could also use viewstate for this. In this case that would probably be a better solution, since the data you are using (your counter) is not user-specific but page-specific.

更多推荐

本文发布于:2023-08-07 16:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   TimerControl   Ajax   variables   Instance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!