动态获取消息并提醒
实现目的:在页面上定时从后台异步获取信息,并根据返回的信息在页面上显示提醒信息。
后台页面实现:
Msg.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Charset = "GB2312";
Response.ContentType = "text/html";
Response.Cache.SetExpires(new DateTime().AddSeconds(1));
Response.Clear();
alertWitch();
}
/// <summary>
/// 显示告警信息,这个地方是根据需要定义的
/// </summary>
private void alertWitch()
{
Dafang.Framework.User user = CoreService.Instance.GetUser(User.Identity.Name);
// 结果 A1,有告警 A0,无告警
string strAlm = "A0";
if (user != null)
{
if(SsfService.Instance.GetRecMessage(user.Username, "1").Count>0) // 有新消息
strAlm = "A1";
}
Response.Write(strAlm);
Response.Flush();
Response.End();
}
前台页面代码,对这个有用的部分:
<html>
<head>
<script type="text/javascript">
// 告警数据
function getAlam() {
if (!document.all.ErrorMessage.innerHTML) {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// 获取信息
xmlhttp.Open("get", "Alarm.aspx", false);
xmlhttp.Send();
var v = xmlhttp.responseText;
// 把提醒信息置为空
eval("ErrorMessage.innerHTML" + "=''");
if (v == "A1") // 要显示提醒
{
v = "<div id='newMessage' name='newMessage' style='visibility:visible;'><table><tr><td class='FormPage'>";
v += "<a target='view' href='Manager/MsgReceiver.aspx' onClick=""javascript:document.all.ErrorMessage.innerHTML='';newMessage.style.visibility = 'hidden'; "" ";
v += "target='_blank' style='color:red;font-size:10pt'><img src='Images/Message.gif' ";
v += "width='15' height='14' align='absmiddle' border='0'>您有新消息!</a><br>";
v += "<bgsound src='images/newMessage.wav' Loop=-1></td></tr></table></div>";
eval("ErrorMessage" + ".innerHTML= v");
}
}
// 一个小时检查一次
setTimeout("getAlam()", 3600000);
}
</script>
</head>
<body onload="getAlam()">
<!-- 显示信息的地方 -->
<span id="ErrorMessage" border="0" style="margin-top:20px"></span>
<body>
后台页面实现:
Msg.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Charset = "GB2312";
Response.ContentType = "text/html";
Response.Cache.SetExpires(new DateTime().AddSeconds(1));
Response.Clear();
alertWitch();
}
/// <summary>
/// 显示告警信息,这个地方是根据需要定义的
/// </summary>
private void alertWitch()
{
Dafang.Framework.User user = CoreService.Instance.GetUser(User.Identity.Name);
// 结果 A1,有告警 A0,无告警
string strAlm = "A0";
if (user != null)
{
if(SsfService.Instance.GetRecMessage(user.Username, "1").Count>0) // 有新消息
strAlm = "A1";
}
Response.Write(strAlm);
Response.Flush();
Response.End();
}
前台页面代码,对这个有用的部分:
<html>
<head>
<script type="text/javascript">
// 告警数据
function getAlam() {
if (!document.all.ErrorMessage.innerHTML) {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// 获取信息
xmlhttp.Open("get", "Alarm.aspx", false);
xmlhttp.Send();
var v = xmlhttp.responseText;
// 把提醒信息置为空
eval("ErrorMessage.innerHTML" + "=''");
if (v == "A1") // 要显示提醒
{
v = "<div id='newMessage' name='newMessage' style='visibility:visible;'><table><tr><td class='FormPage'>";
v += "<a target='view' href='Manager/MsgReceiver.aspx' onClick=""javascript:document.all.ErrorMessage.innerHTML='';newMessage.style.visibility = 'hidden'; "" ";
v += "target='_blank' style='color:red;font-size:10pt'><img src='Images/Message.gif' ";
v += "width='15' height='14' align='absmiddle' border='0'>您有新消息!</a><br>";
v += "<bgsound src='images/newMessage.wav' Loop=-1></td></tr></table></div>";
eval("ErrorMessage" + ".innerHTML= v");
}
}
// 一个小时检查一次
setTimeout("getAlam()", 3600000);
}
</script>
</head>
<body onload="getAlam()">
<!-- 显示信息的地方 -->
<span id="ErrorMessage" border="0" style="margin-top:20px"></span>
<body>
</html>
转自我的163博客