简体   繁体   English

如何间隔刷新页面的一部分

[英]how to refresh a portion of a page in intervals

I have a page that above it I want to have a panel that automatically refresh every 5 minutes, and if a user has a message in his inbox show to him. 我上方有一个页面,我想要一个面板,该面板每5分钟自动刷新一次,并且如果用户在收件箱中看到一条消息向他显示。 What is the best solution for this? 最好的解决方案是什么?

Should I usel AJAX, jQuery, or JavaScript? 我应该使用AJAX,jQuery还是JavaScript? My preferred solution is server side solution. 我首选的解决方案是服务器端解决方案。

I would use Jquery to send an AJax request to the server to fetch the updated content. 我将使用Jquery向服务器发送AJax请求以获取更新的内容。 Upon receiving the content I would use JQuery again to update the markup. 收到内容后,我将再次使用JQuery来更新标记。

You can set this up to trigger every 5 min using setInterval in Javascript 您可以使用Javascript中的setInterval将其设置为每5分钟触发一次

Hard to give a specific example without code. 没有代码很难给出具体示例。 But you basically need to load the new content with an ajax request that is triggered by a timer. 但是,您基本上需要使用由计时器触发的ajax请求来加载新内容。

setInterval(function(){
    $.ajax({
      url: "someUrlThatServesUpdatedContent.aspx",
      cache: false
    }).done(function( html ) {
      $("#results").html(html);
    });
}, 300000);

The above is just a simple example to point you in the right direction. 上面只是一个简单的示例,可为您指明正确的方向。

Edit: Here is an example of how to do the ajax call without JQuery https://stackoverflow.com/a/2792721/1059001 编辑:这是一个如何在不使用jQuery的情况下进行ajax调用的示例https://stackoverflow.com/a/2792721/1059001

It's possible to do that with AJAX using method described by previous answers, but if you wish to have a server side solution I would recommend loading that part of the page in an iframe, with meta refresh: 可以使用前面的答案描述的方法来使用AJAX进行此操作,但是如果您希望使用服务器端解决方案,建议您将页面的该部分加载到iframe中,并进行元刷新:

<meta http-equiv="refresh" content="300">

This method however would make it difficult to communicate any events or user actions back to main page. 然而,这种方法将使得很难将任何事件或用户动作传达回主页。

Since you are working with ASP.Net, you can also achieve this behavior by using a combination of the following: 由于使用的是ASP.Net,因此还可以通过组合使用以下方式来实现此行为:

  1. ScriptManager or ScriptManagerProxy (if you have nested pages): Manages the ajax calls ScriptManagerScriptManagerProxy (如果您有嵌套页面):管理ajax调用
  2. UpdatePanel : Determines what gets updated. UpdatePanel :确定更新的内容。 Controls nested within <ContentTemplate> are subject to partial updates 嵌套在<ContentTemplate>中的控件会进行部分更新
  3. Triggers : Controls when the content is updated. 触发器 :控制何时更新内容。

For your purpose, a Timer control can be used as a trigger to ensure that partial postbacks are triggered every 5 seconds: 为了您的目的,可以将Timer控件用作触发器,以确保每5秒触发部分回发:

<asp:ScriptManager ID="scriptManagerMain" runat="server"/>
<asp:Timer ID="timer" Interval="5000" runat="server"/>
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Panel ID="panelToBeUpdated" runat="server">
            <asp:Label ID="lblContent" runat="server" ></asp:Label>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer" />
    </Triggers>
</asp:UpdatePanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM