Problem with ScriptManager and ASP.NET AJAX Timer
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,
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.
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
{ }
}
精彩评论