Asp.net 计时器不增加计时器刻度中的变量

Asp.net 计时器不增加计时器刻度中的变量,asp.net,vb.net,Asp.net,Vb.net,我使用计时器来递增计数变量 在前面,我使用带有updatepanel的计时器来更新像这样的ModalPopupXtender <asp:Timer runat="server" id="Timer2" Interval="1000" OnTick="Timer2_Tick" Enabled ="false"> </asp:Timer> <asp:UpdatePanel runat="server" id="Update

我使用计时器来递增计数变量

在前面,我使用带有updatepanel的计时器来更新像这样的ModalPopupXtender

       <asp:Timer runat="server" id="Timer2" Interval="1000" OnTick="Timer2_Tick" Enabled ="false"> </asp:Timer>
                 <asp:UpdatePanel runat="server" id="UpdatePanel1">
                  <Triggers>
        <asp:AsyncPostBackTrigger  ControlID="Timer2" EventName="Tick" />
    </Triggers>
<ContentTemplate>
        <div id="Pingdialog">
        <asp:Panel ID="pn1" runat="server"  Style="display: none; background-color:White; border: solid 1px silver;" >
        <asp:Panel ID="pn2" runat="server"  Style="cursor: move; background-color: silver;
            border: solid 1px Gray; color: Black; width:300px;" >
            <div style="width:100%;" >BizView : </div>
        </asp:Panel>
        <div style="text-align:center; width:100%; margin-top:20px; font-size:18px; ">
            <asp:Label ID="lblpingmes" runat="server" Text=""></asp:Label>
                        <asp:textbox ID="txtpingmes" runat="server" Text="" TextMode="multiline" AutoPostBack="true"  Visible="false"></asp:textbox>

            <p style="text-align: center;">
            <asp:Button ID="Button1" runat="server" Text="OK" Width="100px" />
            </p>
        </div>
        </asp:Panel>
        <div style="display: none;"><asp:Button ID="Button2" runat="server" Text="." /></div>
        <asp:ModalPopupExtender ID="modalping" runat="server" TargetControlID="button2"
        PopupControlID="pn1" BackgroundCssClass="modalBackground" OkControlID="Button1"
        OnOkScript="onOk()" DropShadow="true" PopupDragHandleControlID="SubPopup" BehaviorID="savepingmodal" />

    </div>   


</ContentTemplate>

</asp:UpdatePanel>
那么当计时器启动时,你会怎么做

Public Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    countpingcheck += 1
    modalping.Show()
    If countpingcheck < 5 Then
        lblpingmes.Text = countpingcheck
    Else
        Timer2.Enabled = False
        checkip()
    End If
End Sub
Public Sub Timer2\u Tick(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理Timer2.Tick
countpingcheck+=1
modalping.Show()
如果countpingcheck<5,则
lblpingmes.Text=计数检查
其他的
Timer2.Enabled=False
checkip()
如果结束
端接头

我有一个变量
countpingcheck
将每刻度增加+1,但它停留在count=
2
上,我不知道为什么,然后我尝试调试它的循环仅0,1,2

,正如我在一条评论中提到的,每次回发都会创建一个新的页面实例,因此
countpingcheck
每次都将重置为0。要解决这个问题,您需要以某种方式在页面上保持它的值。一种方法是在
UpdatePanel
中使用隐藏的恶魔

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <Triggers>
        <asp:AsyncPostBackTrigger  ControlID="Timer2" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:HiddenField ID="hiddenCountPingCheck" runat="server" />
        <!-- You other controls would be here; skipped for brevity -->
    </ContentTemplate>
</asp:UpdatePanel>

然后在代码背后:

Public Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Dim persistedCountPingCheck as Int32
    If Int32.TryParse(hiddenCountPingCheck.Value, persistedCountPingCheck) Then
        countpingcheck = persistedCountPingCheck
    End If
    countpingcheck += 1
    hiddenCountPingCheck = countpingcheck.ToString()
    modalping.Show()
    If countpingcheck < 5 Then
        lblpingmes.Text = countpingcheck
    Else
        Timer2.Enabled = False
        checkip()
    End If
End Sub
Public Sub Timer2\u Tick(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理Timer2.Tick
Dim持久计数检查为Int32
如果Int32.TryParse(hiddenCountPingCheck.Value,persistedCountPingCheck),则
countpingcheck=持久化countpingcheck
如果结束
countpingcheck+=1
hiddenCountPingCheck=countpingcheck.ToString()
modalping.Show()
如果countpingcheck<5,则
lblpingmes.Text=计数检查
其他的
Timer2.Enabled=False
checkip()
如果结束
端接头

如果有语法问题,我道歉;我的VB有点生疏。

请记住,每次回发邮件时都会创建一个新的页面实例,因此,
countpingcheck
将在每个刻度重置为0,除非您以某种方式将其值保留在页面上。最好的方法是为每个会话使用计时器实例。会话结束后,也处理计时器对象。如果您需要详细信息,请告诉我。谢谢@AJRichardson我遵照您的建议,通过将
dim
更改为
shared
解决了这个问题,这就是工作!我要提醒大家不要共享,因为它将在您网站的所有用户之间共享。我会试着用不同的方法发布一个答案。。。
Public Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Dim persistedCountPingCheck as Int32
    If Int32.TryParse(hiddenCountPingCheck.Value, persistedCountPingCheck) Then
        countpingcheck = persistedCountPingCheck
    End If
    countpingcheck += 1
    hiddenCountPingCheck = countpingcheck.ToString()
    modalping.Show()
    If countpingcheck < 5 Then
        lblpingmes.Text = countpingcheck
    Else
        Timer2.Enabled = False
        checkip()
    End If
End Sub