在一個線上客服系統中,使用了Ajax技術實現無重新整理的資訊更新。下面這個例子是定時清理不線上的客戶
首先在load事件中註冊
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
Ajax.Utility.RegisterTypeForAjax(typeof(Project.www.SC.GuestTalk)); //這裡註冊了類名
}
//===========伺服器事件,由網頁用戶端發送請求============
/// <summary>
/// 清理啟用時間過20分鐘的使用者
/// </summary>
/// <returns></returns>
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public string ClearGuest()
{
DakingConfig dakingConfig = Config.GetSettings();
foreach(DataRow drGuest in Global.DtGuestList.Rows)
{
TimeSpan ts = System.DateTime.Now - Convert.ToDateTime(drGuest["ActiveTime"]);
if(ts.Minutes>= Convert.ToInt32(dakingConfig.TimeOut))
{
Global.DtGuestList.Rows.Remove(drGuest);
DataRow[] drGuestTalks = Global.DtChatList.Select("GuestId=" + drGuest["GuestId"].ToString());
foreach(DataRow drTalk in drGuestTalks)
{
Global.DtChatList.Rows.Remove(drTalk);
}
}
}
return "true";
}
//================網頁用戶端請求代碼=================
<script language="javascript">
var talks = document.getElementById("Talks");
//Talks是網頁上的標籤,用於顯示該請求返回的字串
function Pop()
{
talks.innerHTML = GuestTalk.GetTalks().value;
GuestTalk.ClearGuest();
}
//20秒自動重新整理一次,20秒取得一次資料.
timer = window.setInterval('Pop()',20000);
</script>