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

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

ContractedBlock.gifExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="f_Test4.aspx.cs" Inherits="f_Test4" %>
None.gif
None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif<head runat="server">
None.gif <title>框架主页</title>
ExpandedBlockStart.gifContractedBlock.gif <script language="javascript" type="text/javascript">dot.gif
InBlock.gif //定时执行按钮事件 查看是否有新的消息
InBlock.gif function fn_BtnEventOnTime()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            document.all.btnhid_RefMsg.click(); 
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        window.οnlοad=function()  
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  
InBlock.gif            window.setInterval("fn_BtnEventOnTime()",20000);  
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif function fn_OpenMsgWindow(varTestID)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif var paraTestID =escape(varTestID);
InBlock.gif            window.open('f_MsgAlert.aspx?&TestIDPara='+paraTestID+'',"","dependent=no,location=no,height=100,width=200,left=0,top=600,");
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedBlockEnd.gif </script>
None.gif</head>
None.gif<body>
None.gif <form id="form1" runat="server">
None.gif <table>
None.gif <tr>
None.gif <td>TestID</td>
None.gif <td>
None.gif <asp:TextBox ID="txt_TestID" runat="server"></asp:TextBox>
None.gif </td>
None.gif <td>TestNote</td>
None.gif <td>
None.gif <asp:TextBox ID="txt_TestNote" runat="server"></asp:TextBox>
None.gif </td>
None.gif </tr>
None.gif </table>
ExpandedBlockStart.gifContractedBlock.gif <%dot.gif--保存按钮 负责写入数据到数据库的f_Test表--%>
None.gif <asp:Button ID="btn_Save" runat="server" Text="保存" OnClick="btn_Save_Click" />
None.gif <br />
None.gif <input id="btnhid_RefMsg" type="button" value="隐藏按钮 刷新消息提示"
None.gif        runat="server" onserverclick="btnhid_RefMsg_ServerClick"
None.gif        style="display:none;" />
None.gif <br />
ExpandedBlockStart.gifContractedBlock.gif <%dot.gif--消息窗口 链接 按钮 将显示页面在iframe中--%>
None.gif <iframe id="ShowPage" ></iframe>
None.gif </form>
None.gif</body>
None.gif</html>

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

ContractedBlock.gifExpandedBlockStart.gif
None.gifusing System;
None.gifusing System.Data;
None.gifusing System.Configuration;
None.gifusing System.Collections;
None.gifusing System.Web;
None.gifusing System.Web.Security;
None.gifusing System.Web.UI;
None.gifusing System.Web.UI.WebControls;
None.gifusing System.Web.UI.WebControls.WebParts;
None.gifusing System.Web.UI.HtmlControls;
None.gif
None.gifpublic partial class f_Test4 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif protected void btn_Save_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif string strCommand1 = " INSERT INTO f_Test(TestID, TestNote , TestState) ";
InBlock.gif        strCommand1 += " VALUES( ";
InBlock.gif        strCommand1 += " " + this.txt_TestID.Text.Trim() + " ";
InBlock.gif        strCommand1 += " ,'" + this.txt_TestNote.Text.Trim() + "' ";
InBlock.gif        strCommand1 += " ,'0' ";//消息状态为0 表示为新消息 需提示
InBlock.gif        strCommand1 += " )";
InBlock.gif        DBClass db1 = new DBClass();
InBlock.gif
InBlock.gif if (db1.RunNotSelectSqlCommand(strCommand1))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            CommClass.DoAlert(this.Page, "新增 保存 成功");
ExpandedSubBlockEnd.gif        }
InBlock.gif else
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            CommClass.DoAlert(this.Page, "新增 保存 失败");
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif protected void btnhid_RefMsg_ServerClick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif string strCommand = " SELECT TOP 1 TestID FROM f_Test ";
InBlock.gif        strCommand += " WHERE TestState='0' ";
InBlock.gif        DBClass db = new DBClass();
InBlock.gif        DataSet ds = db.RunSelectGetDataSet(strCommand);
InBlock.gif if (ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif string strTestID = ds.Tables[0].Rows[0][0].ToString().Trim();
InBlock.gif string strJS = "<script language='javascript' type='text/javascript'>";
InBlock.gif            strJS += " fn_OpenMsgWindow('" + strTestID + "'); ";
InBlock.gif            strJS += "</script>";
InBlock.gif            Page.RegisterStartupScript("winOpenJS", strJS);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

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

ContractedBlock.gifExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="f_MsgAlert.aspx.cs" Inherits="f_MsgAlert" %>
None.gif
None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif<head runat="server">
None.gif <title>消息提示</title>
ExpandedBlockStart.gifContractedBlock.gif <script language="javascript">dot.gif
InBlock.gif <!--
InBlock.gif function clock()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif        i=i-1
InBlock.gif        document.title="消息提示 "+i+"秒后关闭!";
InBlock.gif if(i>0)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            setTimeout("clock();",1000);
ExpandedSubBlockEnd.gif        }
InBlock.gif else
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            window.returnValue = '';
InBlock.gif            window.opener = null;
InBlock.gif            window.close();
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif var i=10
InBlock.gif    clock();
InBlock.gif //-->
InBlock.gif
InBlock.gif function openLink()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif var pageURL = "f_Test1.aspx";
InBlock.gif//      window.opener.document.getElementById("txt_TestID").value = "abc";
InBlock.gif        window.opener.document.frames("ShowPage").window.location.href = pageURL;
InBlock.gif        window.returnValue = '';
InBlock.gif        window.opener = null;
InBlock.gif        window.close();        
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif </script>
None.gif</head>
None.gif<body>
None.gif <form id="form1" runat="server">
None.gif <table>
None.gif <tr>
None.gif <td>TestID</td>
None.gif <td>
None.gif <asp:TextBox ID="txt_TestID" runat="server"></asp:TextBox>
None.gif </td>
None.gif </tr>
None.gif <tr>
None.gif <td>TestNote</td>
None.gif <td>
None.gif <asp:TextBox ID="txt_TestNote" runat="server"></asp:TextBox>
None.gif </td>
None.gif </tr>
None.gif </table>
None.gif <asp:Button ID="btn_Readed" runat="server" Text="已读" />
None.gif <asp:Button ID="btn_Link" runat="server" Text="链接" OnClientClick="openLink();" />
None.gif <asp:Button ID="btn_Delete" runat="server" Text="删除" />
None.gif </form>
None.gif</body>
None.gif</html>

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

ContractedBlock.gifExpandedBlockStart.gif
None.gifusing System;
None.gifusing System.Data;
None.gifusing System.Configuration;
None.gifusing System.Collections;
None.gifusing System.Web;
None.gifusing System.Web.Security;
None.gifusing System.Web.UI;
None.gifusing System.Web.UI.WebControls;
None.gifusing System.Web.UI.WebControls.WebParts;
None.gifusing System.Web.UI.HtmlControls;
None.gif
None.gifpublic partial class f_MsgAlert : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if(!string.IsNullOrEmpty(Request["TestIDPara"]))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif string strCommand = " SELECT TOP 1 TestID ,TestNote FROM f_Test ";
InBlock.gif                strCommand += " WHERE TestID='" + Request["TestIDPara"].Trim() + "' ";
InBlock.gif                DBClass db = new DBClass();
InBlock.gif                DataSet ds = db.RunSelectGetDataSet(strCommand);
InBlock.gif if (ds.Tables[0].Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif this.txt_TestID.Text = ds.Tables[0].Rows[0]["TestID"].ToString().Trim();
InBlock.gif this.txt_TestNote.Text = ds.Tables[0].Rows[0]["TestNote"].ToString().Trim();
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

转载于:https://www.cnblogs.com/wuhen/articles/1410872.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值