刷新数据库更改页面 [英] refresh page on database change

查看:131
本文介绍了刷新数据库更改页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我将在这里发布我的下一个问题,因为我不知道从哪里开始。我的问题是:

我正在尝试这个。我有一个页面,我从那里读取表中的所有记录。当管理员向数据库添加新记录时,所有访问者都应该获得刷新请求,否则他们应该自动刷新。


a更实用的例子(更好地解释我的意思)无线电地点保持他们的索引页面上的播放列表(从他们上次播放的歌曲开始)。当无线电台开始新歌时,它会被添加到数据库中。然后,当时访问主页面的所有用户都应刷新浏览器。


我现在所做的是读取歌曲的时间并将其填入标题(刷新 ;)但我认为这可以做得更好。


有没有人知道如何做到这一点?

解决方案

基于C#的示例

您应该尝试查看ASP.NET生命周期并了解 - 其中一个页面事件应该调用您用来加载数据库的方法首先。


这个网站很好:
http://www.15seconds.com/issue/020102.htm


如果我这样做,我会去OnPreRender()方法并将您想要调用的代码放在IsPostBack子句中,然后每次都要调用所有代码,包括在子句之外加载数据库的方法,li ke so:

展开 | 选择 | Wrap | 行号


所以,如果我理解正确,我需要在postback子句中执行一次所有事情,所以我不执行两次(例如插入查询)我读取数据库的查询将在本条款之外。


到目前为止这么好,但是如何触发像onDatabaseChange.or我如何添加一个changelistener?


已经tnx,我以前从未使用过ASP.NET,因此我不知道如何在其中编写任何代码。

@Ciary


Web应用程序(如ASP.NET Web应用程序)是无状态应用程序。


这是相当有限的......让我们来看一下:


Web服务器上存在ASP.NET应用程序。它们被编译(除非它们已经被预编译)并在浏览器发出涉及它们的请求时执行。


ASP.NET应用程序做东西(如从数据库中检索东西等)然后将结果响应(网页)发送到浏览器......向最终用户显示网页。


现在管理员制作更改数据库...并且您的数据访问层引发了onDatabaseChange事件....


即使您的ASP.NET应用程序正在侦听事件,它应该做什么用它?


ASP.NET应用程序向浏览器发送信息的唯一方法是首先向它发出请求...否则它将如何知道哪里(什么)浏览器,什么IP)发送给它?如果网络浏览器不等待来自服务器的响应,它甚至会如何发送响应?


因为它不能知道这些事情,所以它不能对事件做任何事情......它不能更新当前显示网页的任何网页浏览器,因为如果没有网页浏览器对网页的请求,它就无法做到这一点。


我的观点是,如果您正在开发一个Web应用程序(您从未说过您正在开发的应用程序的类型),在Web浏览器向Web浏览器发出请求之前,数据库是否已更改无关紧要Web应用程序。


如果您实现Web应用程序以便访问浏览器发出的每个请求的实时数据库(包含更新的信息),则不需要onDatabaseChange事件。


这意味着你不会在第一次加载页面时缓存从数据库中检索到的数据。


有一种巧妙的方式在ASP.NET中缓存您的数据...


请参阅:

如果你只是简单的话就容易多了使用实时数据库并在每个Web请求上刷新数据。


为了实现您的目标(在数据库更新时更新显示网页的所有浏览器) ),你必须经常向服务器询问更新列表。您将不得不拥有某种客户端(JavaScirpt / Ajax)计时器,它会不断地对服务器进行ping操作,因为Web服务器无法在没有浏览器要求的情况下向浏览器发送响应。这可能不是一个好主意,因为它会浪费带宽和服务器资源。


hey all,

i''m going to post my next question here since i have no idea where to start. my problem is this:
i''m trying to this. i have a page from where i read all records from a table. when the admin adds a new record to a database, all visitors should get a refresh request or they should automatically refresh.

a more practical example (to better explain what i mean) a radiostation keeps a playlist (starting from their last played song) on their index page. when the radiostation starts a new song, it is added to the database. then, all users visiting the main page at that moment should refresh their browser.

what i do now is read the time of the song and fill it in into a header("refresh") but this can be done a lot better i think.

does anyone have any idea how this can be done?

解决方案

An example based on C#

You should try looking at the ASP.NET Life cycle and understand - one of the page events should call the method which you used to load the database in the first place.

This site is good:
http://www.15seconds.com/issue/020102.htm

If I was doing this, I would go for the OnPreRender() method and put the code you want to be called ONCE in an IsPostBack clause, then all the code you want to be called each time, including your method to load the database outside the clause, like so:

Expand|Select|Wrap|Line Numbers


so if i understand correctly i need all things executed once in the postback clause so i dont execute things twice (like insert queries for example) the query in which i read the database will be outside this clause.

so far so good, but how do i trigger events like onDatabaseChange.or how do i add a changelistener to it??

already tnx, ive never used ASP.NET before so i have no idea how to code anything in it.


@Ciary
Web applications (like an ASP.NET web application) are stateless apps.

This is rather limiting....let''s examine this a bit:

ASP.NET applications exist on a Web Server. They are compiled (unless they are already precompiled) and executed when a browser makes a request that involves them.

The ASP.NET application does stuff (like retrieves things from databases etc) and then sends a the resulting response (a web page) to the browser...which displays the web page to the end user.

Now the admin makes a change database...and your data access layer raises an onDatabaseChange event....

Even if your ASP.NET application was listening for the event, what is it supposed to do with it?

The only way that the ASP.NET application can send information to the browser is if a request was made to it in the first place... Otherwise how would it know where (what browser, what IP) to send it to? How would it even send the response if the web browser isn''t waiting for a response from the server?

Since it can''t know these things, it can''t do anything with the event...it can''t update any web browsers currently displaying the web page because it simply cannot do this without a request from the web browser for the page.


My point here is if you are developing a web application (you never stated the type of app you''re developing) it doesn''t matter if the database has changed until a web browser makes a request to the web application.

The onDatabaseChange event is not needed if you implement the web application so that it accesses a live database (which contains the updated information) on every request is made by the browser.

This would mean that you would not cache the data retrieved from the database first time the page is loaded.

There is a neat way to cache your data...in ASP.NET though.

See:
It''s a lot easier if you just simply use a live database and refresh your data on every web request.


In order to achieve what you are attempting (updating all browsers displaying the web page when the database is updated), you''d have to constantly ask the server for an updated list. You''ll have to have some sort of client side (JavaScirpt/Ajax) timer that pings the server constantly since the web server cannot send a response to a browser without the browser asking for it. This is probably not a good idea because it''s going to waste bandwidth and server resources.


这篇关于刷新数据库更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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