在一个在线客服系统中,使用了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" type="text/javascript"> var talks = document.getElementById("Talks"); function MyShow() { //2秒自动刷新一次,2秒取得一次数据. timer = window.setInterval('Pop()',RefreshTime); } function Pop() { talks.innerHTML = GuestTalk.GetTalks().value; GuestTalk.ClearGuest(); } </script> <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>