ScriptManager 和 ASP.NET AJAX 计时器的问题

发布于 2024-09-12 15:39:28 字数 1603 浏览 5 评论 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();
}

当我第一次调用 javascript 函数“UpdateValue”时(在 onload 页面事件中),它可以正常工作。但在 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 事件有什么问题?

非常感谢,

I'm having a serious problem with my ASP.NET AJAX application.

There is a javascript function in my application needs to be executed after the Timer_Tick event. Here's the code behind:

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

When I call the javascript function 'UpdateValue' for the first time (at onload page event), it works correctly. But after the Timer_Tick event, it does nothing. This is the HTML code:

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

What's the problem with the ScriptManager or the Timer_Tick event?

Many thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-09-19 15:39:28

看起来您每次 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”应该是一个唯一的键,所以我刚刚更新了该部分。

It looks like you're registering the UpdateValue function each time Timer1_Tick executes.

Try changing your SetValues function to this:

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

EDIT: Notice that I'm using RegisterClientScriptBlock instead of RegisterStartupScript. Also, "myscript" should be a unique key, so I just updated that part.

你的他你的她 2024-09-19 15:39:28

以下是我完成的代码:

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

Following is my finished code:

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 
   { }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文