一切基於項目。
客戶項目中需要對用戶進行實時提醒。
谷歌,度娘問了下。大致有了思路。
通過Ajax方式實現(Jquery+ashx)
通過Jquery的$.post連接ashx取得相應數據,拼接成string字符串返回,然后用JquerUI插件顯示(本文簡化了,用Alert代替)。
aspx代碼如下:
//此代碼應放置在框架頁中
function TimeFunction() {
GetMsg();
window.setInterval("GetMsg()", 240000); //每隔240000ms執行一次查詢
}
function GetMsg() {
var userId = $("#<%=hfUId.ClientID %>").val();
//ajax方式獲取數據
$.post("GetMsgHandler.ashx", { Action: "getMsg", userid: userId }, function (data) {
if (data != "") {
alert(data);
// 用JqueryUI控件的寫法:
//$.messager.show('<font color=red>系統消息</font>', '<div style="word-wrap: break-word; ">' + data + '</div>', 20000);
}
});
}
TimeFunction() ;
ashx 代碼如下:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string resMsg1 = "";
GetMsg(context, out resMsg1);//此方法用於去數據庫查詢數據,然后返回結果
context.Response.Write(resMsg1);
}