计时器不在计时器滴答中递增变量

时间:2015-05-13 02:21:06

标签: asp.net vb.net

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

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

       <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>

中的代码

我使用btn来enabled计时器事件

Partial Class 
    Dim countpingcheck As integer

    Protected Sub btnPing_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPingATM.Click
        pinglog(lblIP.Text.Trim)
        modalping.Show()
        Timer2.Enabled = True

    End Sub

然后当计时器启动时

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

我有一个变量countpingcheck每个刻度会增加+1,但是它被卡在count = 2我不知道为什么,然后我试着调试它的循环只是0,1,2 < / p>

1 个答案:

答案 0 :(得分: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

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