web检测sql数据库数据发生变化 asp.net实时监测数据库内容是否更新 asp.net 如何监视数据库表中某个字段值的改变 asp.net可以做定时器吗 asp.net web实现定时器功能 asp.net中Timer定时器在web中无刷新的使用 C#asp.net 实时监测数据库内容
aspx页面中的代码:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
update中的:<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
当前秒:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
后台cs服务器上的代码:
protected void Timer1_Tick(object sender, EventArgs e)
{
//这里可以操作你想做的事情,比如定时查询数据库
Label1.Text = DateTime.Now.ToLongTimeString()+":"+DateTime.Now.Millisecond;
Label2.Text = DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond;
string sql = "select * from Hua where price =1";
var a=DBHlper.GetDataTable(sql);
if (a.Rows.Count > 0)
{
Label2.Text = a.Rows[0][1]+"警告,老人摔倒";
}
}
说明一下:
要想使用Timer定时器进行无刷新技术实现。首先在aspx页面代码中必须使用ScriptManager控件,同时使用UpdatePanel控件,这样才会达到想要的结果。
测试后,你会发现,在UpdatePanel中的Label2对应的时间在不停的变化但是Lable1只是在你第一次打开页面显示不变化
如果要进行告警弹出后续处理 可使用redect
if (Label2.Text != "1")
{
Response.Redirect("About.aspx?lid="+lid);
}
about页面处理
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["lid"] != null)
{
var id = int.Parse(Request.QueryString["lid"]);
//Response.Write("<script>alert('告警成功!!');</script>");
Response.Write("<script>alert('告警成功!!');window.location.href ='Default.aspx'</script>");
DBHlper.ExecuteNonQuery("update Hua set price =0 where id=" + id);
//Response.Redirect("Default.aspx");
}
//Response.Redirect("Default.aspx");
}