2013-03-22 130 views
0

我在簡單的任務大問題...在asp.net頁面是多視點與兩個視圖,調用C#功能時,AJAX定時器(倒計時)顯示0

在第一查看我的AJAX定時器,計數秒從60到0.時間顯示在標籤上,在updatepanel中。我會打電話給我的C#函數,它會做一些事情,並最終更改活動視圖。

我該怎麼做?

我試圖檢查Timer_tick事件,如果秒== 0,我調用函數,但它不工作。 我也嘗試Timer1.Enabled = false,但它不工作。

我想,我必須使用Javascript,但是如何?我不知道在哪裏以及如何...我還不知道Javascript。

這是我Timer_Tick事件(時間標籤上正確顯示)

protected void Timer1_Tick(object sender, EventArgs e) 
    { 
     Test t = (Test)Session["SelectedTest"]; 

     if (t.Remaining.Minute == 0 && t.Remaining.Second == 0) 
     { 
      DoSomething(); 
     } 
     else 
     { 
      t.Remaining= t.Remaining.AddSeconds(-1); 
      Label7.Text = t.Remaining.ToLongTimeString(); 
     } 
    } 

和我的DoSomething的()函數:

 public void DoSomething() 
     { 

// Doing a lot of things.... 

      MultiView1.ActiveViewIndex = 3; 
     } 

功能DoSomething的正常工作,我也有按鈕調用這個函數 - 它的工作但我也想打電話功能,如果剩餘秒數== 0.

+0

你試過用'if(t.Remaining.Minute <= 0 && t.Remaining.Second <= 0)'嗎? – Coder 2013-03-23 08:57:35

回答

0

是的,我已經試過這個。調試後,我知道,我的C#函數DoSomething()正在工作 - 但最後,視圖沒有改變。調用DoSomething的()後,計數器計時再次...

我發現js函數:

function stopTimer() 
{ 
     var timer = $find("<%=Timer1.ClientID%>") 
    timer._stopTimer(); 
} 

但我不知道,我在哪裏可以調用它。

編輯:我看到,我的函數DoSomething()多次調用。如果定時器顯示0,函數在每個蜱

EDIT2調用:菜鳥溶液:)

  1. 添加第二定時器,設定Timer2.Interval = 9999999(或其他高)
  2. 在Timer1_Tick事件:

    t.Remaining = t.Remaining.AddSeconds(-1); Label7.Text = t.Remaining.ToLongTimeString();在Timer2_Tick

     if (t.Remaining.Minute == 0 && t.Remaining.Second == 1) Timer2.Interval = 1000; 
    
  3. 調用C#功能

原因:如果定時器是的UpdatePanel的觸發器,不能從Tick事件調用C#功能

**我知道,那是更好解決方案做到這一點(可能是JavaScript),但我的截止日期很快,所以我必須使用它。