0

我的 ASP.NET AJAX 应用程序存在严重问题。

我的应用程序中有一个 javascript 函数需要在 Timer_Tick 事件之后执行。这是后面的代码:

void SetValues()
   {
   try 
   {
   StringBuilder sbScript = new StringBuilder();
   sbScript.Append("<script language='javascript' type='text/javascript'>");
   sbScript.Append("function UpdateValue() {");
   for (int j = 0; j < iTotalDevices; j++)
   {
   sbScript.Append("setElementValue(" + j.ToString() + "," + DevicesInfo[j].X.ToString() + "," + DevicesInfo[j].Y.ToString() + "," + iStatus.ToString() + "," + DevicesInfo[j].DeviceID.ToString() + ");");
   }
   sbScript.Append("}");
   sbScript.Append("</script>");
   ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", sbScript.ToString(), false);
   }
   catch 
   { }
   }

protected void Timer1_Tick(object sender, EventArgs e)
{
    ///This function will get latest values from database
    GetNewData();
    SetValues();
}

当我第一次(在 onload 页面事件中)调用 javascript 函数“UpdateValue”时,它可以正常工作。但是在 Timer_Tick 事件之后,它什么也不做。这是 HTML 代码:

<script type="text/javascript" language="javascript">
function PageLoad() {
///Call function for the first time and it works
UpdateValue();
}

function setElementValue(index, value1, value2, value3...) {
///Set value for each element in object array
}

</script>
<body onload="PageLoad()">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="30000">
</body>

ScriptManager 或 Timer_Tick 事件有什么问题?

非常感谢,

4

2 回答 2

2

每次 Timer1_Tick 执行时,您似乎都在注册 UpdateValue 函数。

尝试将您的 SetValues 函数更改为:

void SetValues()
{
   try 
   {
       StringBuilder sbScript = new StringBuilder();
       sbScript.Append("<script language='javascript' type='text/javascript'>");
       for (int j = 0; j < iTotalDevices; j++)
       {
       sbScript.Append("setElementValue(" + j.ToString() + "," + DevicesInfo[j].X.ToString() + "," + DevicesInfo[j].Y.ToString() + "," + iStatus.ToString() + "," + DevicesInfo[j].DeviceID.ToString() + ");");
       }
       sbScript.Append("</script>");
       ScriptManager.RegisterClientScriptBlock(this, this.GetType(), string.Format("myscript{0}", DateTime.Now.ToString("yyyyMMddHHmmss")), sbScript.ToString(), false);
   }
   catch 
   { }
}

编辑:请注意,我使用的是 RegisterClientScriptBlock 而不是 RegisterStartupScript。另外,“myscript”应该是唯一的键,所以我刚刚更新了那部分。

于 2010-08-04T14:33:16.457 回答
0

以下是我完成的代码:

void SetValues()
   {
   try 
   {
   StringBuilder sbScript = new StringBuilder();
   sbScript.Append("<script language='javascript' type='text/javascript'>");

   sbScript.Append("Sys.Application.add_load(UpdateValue);");

   sbScript.Append("function UpdateValue() {");
   sbScript.Append("Sys.Application.remove_load(UpdateValue);");
   for (int j = 0; j < iTotalDevices; j++)
   {
   sbScript.Append("setElementValue(" + j.ToString() + "," + DevicesInfo[j].X.ToString() + "," + DevicesInfo[j].Y.ToString() + "," + iStatus.ToString() + "," + DevicesInfo[j].DeviceID.ToString() + ");");
   }
   sbScript.Append("}");
   sbScript.Append("</script>");
   ScriptManager.RegisterStartupScript(this, Time1.GetType(), "UpdateValue", sbScript.ToString(), false);
   }
   catch 
   { }
   }
于 2010-08-06T02:16:39.983 回答