我在一個WCF服務項目中有一個aspx web窗體,該項目給出了該服務的狀態報告。目前我有一個刷新按鈕,但我想自動定期刷新。定期刷新asp控件
我已經試過兩種方式:
- 一個JavaScript裏面
<head>
使用setInterval()
和Page_Load
開始RegisterStartupScript
。 - A
Systems.Timers.Timer
裏面Page_Load
,做按鈕點擊會做什麼。
我沒有得到(1)正確的,因爲一個明顯的範圍問題,我很明顯誤解它應該如何工作。目前腳本只是做一個警報,我不能進一步。
我不明白(2)是正確的,儘管看起來好像它應該起作用,因爲將一個斷點放入定時器函數中會顯示正確的值,但頁面上的表單標籤不會更新。
的webform.aspx如下所示:在webform.aspx.cs
<%@ 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>
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
其正確包含由該服務的其他地方填充的狀態。
我很難過,要麼是方法正確,我該如何繼續?
的AJAX計時器方法是我最終使用。 – acraig5075