ScriptManager 和 ASP.NET AJAX 计时器的问题
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您每次 Timer1_Tick 执行时都会注册 UpdateValue 函数。
尝试将您的 SetValues 函数更改为:
编辑:请注意,我使用的是 RegisterClientScriptBlock 而不是 RegisterStartupScript。另外,“myscript”应该是一个唯一的键,所以我刚刚更新了该部分。
It looks like you're registering the UpdateValue function each time Timer1_Tick executes.
Try changing your SetValues function to this:
EDIT: Notice that I'm using RegisterClientScriptBlock instead of RegisterStartupScript. Also, "myscript" should be a unique key, so I just updated that part.
以下是我完成的代码:
Following is my finished code: