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:
- A javascript inside
<head>
usingsetInterval()
and started inPage_Load
withRegisterStartupScript
. - A
Systems.Timers.Timer
insidePage_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?