如何刷新页面时在db表记录在ASP.Net 3.5得到改变 [英] How to refresh a page when record in db table get changed in ASP.Net 3.5

查看:195
本文介绍了如何刷新页面时在db表记录在ASP.Net 3.5得到改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个GridView显示从数据库表例如记录页面tblEmployee。
在该表中如果一个记录被插入/更新/删除,那么我需要我的页面重新加载,这样在GridView将显示更新的记录。记录可被插入/更新/从任何其他应用程序中删除。
重新绑定的GridView我必须重新加载页面。假设我在浏览器中打开页面,并没有回发完成。 10分钟后记录是否已被其他应用程序插入到表中。我如何才能通过点击刷新按钮当数据库表中的记录自动改变重新加载页面,而不是手动?我使用SQL Server 2005作为DB和ASP.Net 3.5(C#)

I have a page that contains a Gridview showing a record from a db table e.g. "tblEmployee". If a record is inserted/updated/deleted in that table then I need my page to be reloaded so that the gridview will show the updated records. Records may be inserted/updated/deleted from any other application. To rebind the gridview I have to reload the page. Suppose I open the page in my browser and no postback is done. After 10 minutes a record is inserted into the table by some other apps. How can I reload the page automatically, not manually by clicking refresh button when the records in db table changed? I am using Sql server 2005 as DB and ASP.Net 3.5(C#)

请建议如何实现这一点。

Please suggest how to implement this.

感谢。

推荐答案

下面是一个使用触发两种方法...一,另一个使用的SqlDependency。我会建议使用的SqlDependency,但我概述另一种方法,因为你用表示的SqlDependency麻烦。

Here's two approaches... one using a trigger, the other using SqlDependency. I would recommend using SqlDependency, but I'm outlining another approach because you indicated trouble with SqlDependency.

创建的表名和最后更新日期列的表。创建tblEmployee触发器来更新任何插入/更新该表/删除。

Create a table with columns for table name and last update date. Create a trigger on tblEmployee to update that table on any insert/update/delete.

在加载页面时,你要存储表的当前上次更新日期。根据您的页面是如何设置,你可以将其存储在ViewState中或在页面上隐藏字段。那么你就需要轮询改变。有很多方法可以做到这一点。你可以写一个返回的最后更新日期一个简单的页面方法,从JavaScript调用它,并将它与你存储在隐藏字段的日期。或者你可以使用一个UpdatePanel与Timer控件,并比较你存储在ViewState中的值。

When the page is loaded, you'll want to store the current last update date for the table. Depending on how your page is setup, you could store it in ViewState or as a hidden field on the page. Then you'll need to poll for changes. There are many ways to do this. You could write a simple Page Method that returns the last update date, call it from JavaScript, and compare it to the date you stored in the hidden field. Or you could use an UpdatePanel with a Timer control and compare to the value you stored in ViewState.

现在这种方式访问​​数据库超过必要的。如果这是一个问题,使用的SqlDependency与SQL 2005或更高版本。创建的SqlDependency和插入当前日期与这种依赖对象的缓存。如上所述从页面查询该高速缓冲存贮器项。当依赖改变(OnChange事件),再次更新缓存项的日期。

Now this approach hits the database more than necessary. If that's a problem, use SqlDependency with SQL 2005 or greater. Create a SqlDependency and insert the current date into the Cache with this dependency object. Poll this cache item from the page as described above. When the dependency changes (OnChange event), update the cache item date again.

这篇关于如何刷新页面时在db表记录在ASP.Net 3.5得到改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆