そのサービスのステータス レポートを提供する WCF サービス プロジェクトに aspx Web フォームがあります。現在、更新ボタンがありますが、定期的な更新を自動化したいです。
私は2つの方法を試しました:
<head>
using内の JavaScript がsetInterval()
で開始されPage_Load
ましたRegisterStartupScript
。- ボタンのクリックが行うことを行う
Systems.Timers.Timer
内部の 。Page_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# コードは次のようになります。これは、目的の更新コードが存在する場所です。もちろん、私は一度に 1 つの方法しか試していません。私が試したことを明確にするために、以下ではどちらもコメントを外しています。
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
私は困惑しています、どちらの方法も正しいですか、どうすれば進めますか?