C#+JS简单实现 定时轮询数据表 以及时弹出消息提示窗口(转)

来源:互联网 发布:万圣节整人软件 编辑:程序博客网 时间:2024/05/29 12:08

实现思路:
框架主页 + 弹出的消息显示页面
框架主页内 通过js定时执行一隐藏按钮的click事件
    其事件为查询消息表
    如有新消息 则在框架页弹出消息提示窗口
消息提示窗口 定时自动关闭
    其内有 已读 和 删除 及 链接 按钮
    链接按钮 负责刷新框架面的iframe中的src
=====================
相关示例代码如下:
----------------
框架页面HTML部分
----------------


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="f_Test4.aspx.cs" Inherits="f_Test4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>框架主页</title>
<script language="javascript" type="text/javascript">
//定时执行按钮事件 查看是否有新的消息
function fn_BtnEventOnTime()
{
            document.all.btnhid_RefMsg.click(); 
        }

        window.onload=function()  
{  
            window.setInterval("fn_BtnEventOnTime()",20000);  
        }

function fn_OpenMsgWindow(varTestID)
{
var paraTestID =escape(varTestID);
            window.open('f_MsgAlert.aspx?&TestIDPara='+paraTestID+'',"","dependent=no,location=no,height=100,width=200,left=0,top=600,");
        }

</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>TestID</td>
<td>
<asp:TextBox ID="txt_TestID" runat="server"></asp:TextBox>
</td>
<td>TestNote</td>
<td>
<asp:TextBox ID="txt_TestNote" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<%--保存按钮 负责写入数据到数据库的f_Test表--%>
<asp:Button ID="btn_Save" runat="server" Text="保存" OnClick="btn_Save_Click" />
<br />
<input id="btnhid_RefMsg" type="button" value="隐藏按钮 刷新消息提示"
        runat="server" onserverclick="btnhid_RefMsg_ServerClick"
        style="display:none;" />
<br />
<%--消息窗口 链接 按钮 将显示页面在iframe中--%>
<iframe id="ShowPage" ></iframe>
</form>
</body>
</html>

-------------------
框架页面.cs代码部分
-------------------


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class f_Test4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)


        }
    }

protected void btn_Save_Click(object sender, EventArgs e)
{
string strCommand1 = " INSERT INTO f_Test(TestID, TestNote , TestState) ";
        strCommand1 += " VALUES( ";
        strCommand1 += " " + this.txt_TestID.Text.Trim() + " ";
        strCommand1 += " ,'" + this.txt_TestNote.Text.Trim() + "' ";
        strCommand1 += " ,'0' ";//消息状态为0 表示为新消息 需提示
        strCommand1 += " )";
        DBClass db1 = new DBClass();

if (db1.RunNotSelectSqlCommand(strCommand1))
{
            CommClass.DoAlert(this.Page, "新增 保存 成功");
        }
else
{
            CommClass.DoAlert(this.Page, "新增 保存 失败");
        }
    }

protected void btnhid_RefMsg_ServerClick(object sender, EventArgs e)
{
string strCommand = " SELECT TOP 1 TestID FROM f_Test ";
        strCommand += " WHERE TestState='0' ";
        DBClass db = new DBClass();
        DataSet ds = db.RunSelectGetDataSet(strCommand);
if (ds.Tables[0].Rows.Count > 0)
{
string strTestID = ds.Tables[0].Rows[0][0].ToString().Trim();
string strJS = "<script language='javascript' type='text/javascript'>";
            strJS += " fn_OpenMsgWindow('" + strTestID + "'); ";
            strJS += "</script>";
            Page.RegisterStartupScript("winOpenJS", strJS);
        }
    }
}

--------------------------
弹出的消息显示页面HTML部分
--------------------------


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="f_MsgAlert.aspx.cs" Inherits="f_MsgAlert" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>消息提示</title>
<script language="javascript">
<!--
function clock()
{
        i=i-1
        document.title="消息提示 "+i+"秒后关闭!";
if(i>0)
{
            setTimeout("clock();",1000);
        }
else
{
            window.returnValue = '';
            window.opener = null;
            window.close();
        }
    }
var i=10
    clock();
//-->

function openLink()
{
var pageURL = "f_Test1.aspx";
//      window.opener.document.getElementById("txt_TestID").value = "abc";
        window.opener.document.frames("ShowPage").window.location.href = pageURL;
        window.returnValue = '';
        window.opener = null;
        window.close();        
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>TestID</td>
<td>
<asp:TextBox ID="txt_TestID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>TestNote</td>
<td>
<asp:TextBox ID="txt_TestNote" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID="btn_Readed" runat="server" Text="已读" />
<asp:Button ID="btn_Link" runat="server" Text="链接" OnClientClick="openLink();" />
<asp:Button ID="btn_Delete" runat="server" Text="删除" />
</form>
</body>
</html>

-----------------------------
弹出的消息显示页面.cs代码部分
-----------------------------


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class f_MsgAlert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!string.IsNullOrEmpty(Request["TestIDPara"]))
{
string strCommand = " SELECT TOP 1 TestID ,TestNote FROM f_Test ";
                strCommand += " WHERE TestID='" + Request["TestIDPara"].Trim() + "' ";
                DBClass db = new DBClass();
                DataSet ds = db.RunSelectGetDataSet(strCommand);
if (ds.Tables[0].Rows.Count > 0)
{
this.txt_TestID.Text = ds.Tables[0].Rows[0]["TestID"].ToString().Trim();
this.txt_TestNote.Text = ds.Tables[0].Rows[0]["TestNote"].ToString().Trim();
                }
            }
        }
    }
}

http://www.cnblogs.com/wuhen/articles/1410872.html
原创粉丝点击