2015-06-09 44 views
-2

我在代碼的函數後面,返回一個標籤lblvisible.text自動刷新功能ASP.NET C#

protected void AutoloadDen() 
{ 
    //somecode 
    lblvisible.Text = //somecode; 
    // i want to autorefresh function AutoloadDen in 5s 
    //Such as: Autorefresh(AutoloadDen,5s) 
} 

回答

0

你的asp.net標記添加一個計時器控件和代碼後面添加其Tick事件。 Timer控件的間隔時間設置爲5000,並在後面的代碼調用AutoloadDean()函數在定時器

+0

Okie使用上serversidetimer的。我受夠了。但是有什麼不對。每5秒鐘發佈一次lblvisible.text。並且,它繼續 我的第一個負載是:001 第二個負載:001001 –

+0

請詳細說明一點。您的評論沒有清除您當前的問題.. –

0

的Tick事件使用System.Windows.Forms.Timer

private Timer timer1; 
public void InitTimer() 
{ 
    timer1 = new Timer(); 
    timer1.Tick += new EventHandler(timer1_Tick); 
    timer1.Interval = 5000; // in miliseconds 
    timer1.Start(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    AutoloadDen(); 
} 

要調用它使用ajax你需要寫一個js功能如下:

$(document).ready(function(){ 
     setTimeout(function(){ 
      $.ajax({ 
       url: "yourpage.aspx/AutoloadDen", 
       method: "GET", 
       dataType: "json", 
       success:function(data){ 
        $('#yourtextboxid').val(data); 
       }, 
       error:function(data){ 
        //Display error message 
       } 
      }); 
     }); 
}); 

在服務器側方法稍加修改

protected void AutoloadDen() 
{ 
    //somecode 
    JavaScriptSerializer serializer = new JavaScriptSerializer() 
    return serializer.Serialize(YourText);  
} 

沒有必要在這種情況下