2011-06-27 41 views
0

當用戶坐在我的頁面上時,每隔5分鐘我需要對數據庫進行Ajax檢查,以查看某些行是否包含特定值。如果一個或多個做了,那麼需要彈出一個簡單的警報。用於檢查數據庫的ASP.NET Ajax

不確定從哪裏開始。

謝謝!

+0

你在起訴什麼?的WebForms? MVC? – balexandre

回答

0

你真的想用ASP.NET AJAX?

如果你想有一個簡單的方法,實現這一點:

1 - 創建一個新的通用處理器(ashx的頁面),稱之爲例如refreshDbValues.ashx

2 - 增加你的代碼,而在最後返回你想要的信息,例如:

public void ProcessRequest (HttpContext context) { 

    string r = "", 
      time = context.Request["t"]; 

    // your code - start 
    r = "There are new results to see"; 
    // your code - end 

    context.Response.ContentType = "text/plain"; 
    context.Response.Write(r); 
} 

3 - 現在,你做你的服務器的一部分,讓我們做的客戶端部分,在你的頁面,添加此jQuery代碼:

var processId = 0; 

$(document).ready(function() { 

    // let's make this method fires every 2 seconds 
    processId = setInterval(requestNewValue, 2000); 

}); 

function requestNewValue() { 

    var now = new Date(); 

    $.ajax({ 
     url: "refreshDbValues.ashx", 
     type: "GET", 
     data: { t: now.getTime() } 
     success: function(data){ 

      // do we have text to show? 
      if(data.length > 0) { 
       alert(data); 
      } 
     }, 
     error: function(msg) { 
      alert('Error found:\n' + msg); 
     } 
    ); 

} 
0

Balexandre's是一個很好的解決方案,對我的應用程序非常有效。

我用它的一個變體與toastr相結合來通知用戶我的web應用程序最近的更新。絕對簡化了我認爲將是一個非常乏味的過程。