2015-06-09 43 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);  
} 

没有必要在这种情况下