0

I have an aspx web form in a WCF service project that gives a status report on that service. Currently I have a Refresh button, but I'd like to automate a regular periodic refresh.

I've tried two ways:

  1. A javascript inside <head> using setInterval() and started in Page_Load with RegisterStartupScript.
  2. A Systems.Timers.Timer inside Page_Load, doing what the button click would do.

I don't get (1) right because of an obvious scope problem and I'm clearly misunderstanding how it should work. Currently the script simply does an alert, and I can't get further.

I don't get (2) right, though it seems as if it should work, because putting a breakpoint into the timer function shows correct values, but the form labels on the page don't update.

The webform.aspx looks as follows:

<%@ 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>

The c# code in the webform.aspx.cs looks as follows. This is where the desired refresh code resides. Of course I only try one method at a time, they're both uncommented below for clarity as to what I've been trying.

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;
        }
    }
}

A Global.asax file contains the static string variable StatusDescription which correctly contains a status that is populated elsewhere by the service.

I'm stumped, are either methods correct, and how do I proceed?

acraig5075
  • 10,588
  • 3
  • 31
  • 50

2 Answers2

0

Change this:

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

To:

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

And see if that works better.

You can also use the timer approach, but not through System.Timers.Timer; use the ASP.NET AJAX timer control.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

I managed to fix this by adding an AJAX Timer from the Toolbox into the UpdatePanel and using it's OnTick property to call RefreshButton_Click

Followed the instructions here.

acraig5075
  • 10,588
  • 3
  • 31
  • 50