简体   繁体   English

为什么在处理ASP.NET Timer Tick事件时索引变量不增加?

[英]Why won't my index variable increment when ASP.NET Timer Tick event is handled?

This is a bit of an interesting question. 这是一个有趣的问题。 I have an ASP.NET Timer ( Timer1 ) that is supposed to increment an integer variable that I am using to keep track of which item in a menu is selected. 我有一个ASP.NET计时器( Timer1 ),该计时器应该增加一个整数变量,用于跟踪选择菜单中的哪个项目。 Have a look at the Tick event handler. 看一下Tick事件处理程序。

'Menu_RightSide.Items(TimerMenuIndex).Selected = True` 'Menu_RightSide.Items(TimerMenuInde​​x).Selected = True`

Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick

    Select Case TimerMenuIndex
            Case 0
                bindDataToChart_locationChrono() 
                TimerMenuIndex += 1
            Case 1
                BindCurrentProgressDataToChart() 
                TimerMenuIndex += 1
            Case 2
                bindDBDataToChart()                   
                TimerMenuIndex += 1
            Case Else
                'Do Something else         
    End Select
End Sub

This seems like it should be relatively simple logic. 这似乎应该是相对简单的逻辑。 All I want is for the TimerMenuIndex variable to increment by one when the Tick event handler function is called. 我想要的是在TimerMenuIndex Tick事件处理程序函数时将TimerMenuIndex变量加1。 However, for some reason, it will not increment.I should mention that TimerMenuIndex is a global variable. 但是由于某种原因,它不会增加。我应该提到TimerMenuIndex是一个全局变量。 I know that the Timer control is AJAX-based, so it shouldn't be calling the Page_Load function when the Timer refreshes the page. 我知道Timer控件是基于AJAX的,因此,当Timer刷新页面时,它不应该调用Page_Load函数。 I can't think of any reason why this doesn't work. 我想不出什么都不行的原因。 I could be missing something very obvious. 我可能会错过一些非常明显的东西。 Any thoughts? 有什么想法吗?

UPDATE: Here is the Page_Load code that pertains to this functionality: 更新:这是与此功能有关的Page_Load代码:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
     Menu_RightSide.Items(TimerMenuIndex).Selected = True
End Sub

What is happening is the variable gets reset to 0 after the tick event. 发生的情况是在滴答事件之后变量被重置为0

You need to declare Shared, this will make the variable retain its value, and 您需要声明Shared,这将使变量保留其值,并且

Shared TimerMenuIndex As Integer = 0

I created a sample application for you to try 我创建了一个示例应用程序供您尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM