【问题标题】:Refresh asp controls periodically定期刷新asp控件
【发布时间】:2012-07-02 20:26:29
【问题描述】:

我在 WCF 服务项目中有一个 aspx Web 表单,该表单提供有关该服务的状态报告。目前我有一个刷新按钮,但我想自动定期刷新。

我尝试了两种方法:

  1. <head> 中使用setInterval() 并在Page_Load 中使用RegisterStartupScript 开始的javascript。
  2. Systems.Timers.TimerPage_Load 内,执行单击按钮的操作。

由于明显的范围问题,我没有正确理解 (1),而且我显然误解了它应该如何工作。目前该脚本只是发出警报,我无法进一步了解。

我没有理解 (2) 正确,尽管它似乎应该可以工作,因为在计时器函数中放置一个断点会显示正确的值,但页面上的表单标签不会更新。

webform.aspx 如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Manage.aspx.cs" Inherits="MyServiceWS.ManageForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Manage</title>
    <script type="text/javascript">
        // method (1)
        RefreshScript = function ()
        {
            setInterval(function () { alert("How to call the Refresh code?"); }, 5000);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <p>
                Synchronisation status: 
                <asp:Label ID="SyncStatusLabel" runat="server" />
                <p>
                <asp:Button ID="RefreshButton" runat="server" Text="Refresh" 
                    onclick="RefreshButton_Click" />
                <p>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

webform.aspx.cs 中的 c# 代码如下所示。这是所需的刷新代码所在的位置。当然,我一次只尝试一种方法,为了清楚我一直在尝试什么,它们都没有在下面注释。

namespace MyServiceWS
{
    public partial class ManageForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // method (1)
            ClientScript.RegisterStartupScript(this.GetType(), "addScript", "RefreshScript()", true);

            if (Page.IsPostBack)
                Refresh();
            else
            { // method (2)
                var myTimer = new System.Timers.Timer();
                myTimer.Elapsed += new ElapsedEventHandler(TimedRefreshEvent);
                myTimer.Interval = 5000;
                myTimer.Start();
            }
        }
        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void TimedRefreshEvent(object source, ElapsedEventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            SyncStatusLabel.Text = Global.StatusDescription;
        }
    }
}

Global.asax 文件包含静态字符串变量StatusDescription,它正确地包含由服务在其他地方填充的状态。

我很困惑,这两种方法都正确吗?我该如何继续?

【问题讨论】:

    标签: asp.net refresh global-asax


    【解决方案1】:

    改变这个:

    RefreshScript = function () 
            { 
                setInterval(function () { alert("How to call the Refresh code?"); }, 5000); 
            } 
    

    收件人:

    function RefreshScript()
    { 
         setInterval(function () { alert("How to call the Refresh code?"); }, 5000); 
    } 
    

    然后看看是否效果更好。

    你也可以使用定时器的方式,但不能通过 System.Timers.Timer;使用ASP.NET AJAX timer control

    【讨论】:

    • 我最终使用的是 AJAX 计时器方法。
    【解决方案2】:

    我设法通过将工具箱中的 AJAX 计时器添加到 UpdatePanel 并使用它的 OnTick 属性调用 RefreshButton_Click 来解决此问题

    按照指示here.

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多