2015-05-13 91 views
2

我使用一個計時器來遞增變量計數計時器不遞增在計時器滴答可變

在前面我使用定時器的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每個時鐘週期,但它停留在數= 2我不知道爲什麼,那我就調試它的循環只是0,1, 2

+1

請記住,每個帖子後面都會創建一個新的頁面實例,所以'countpingcheck'將每個tick復位爲0,除非您以某種方式將其值保存在頁面上。 –

+0

最好的方法是爲每個會話使用計時器實例。一旦會話結束,也配置定時器對象。如果你需要的細節讓我知道。 – vendettamit

+0

謝謝@AJRichardson我遵循你的建議,我通過從'昏暗'改爲'共享'解決了它,它的工作! –

回答

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有點生疏。

+0

謝謝@AJ! –

相關問題