views:

33

answers:

2

Hello,

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,

+1  A: 

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.

Tchami
Thank you Tchami, I've tried the code snip ScriptManager.RegisterClientScriptBlock as your suggestion, but it still did not work.I also viewed the page source and noticed that the first-time called function has been injected at the end of the page:<script language='javascript' type='text/javascript'>function UpdateValue() { setElementValue(...); setElementValue(...); etc }</script>
Mr. Smiley
So if the next function myscript{0} was registered correctly, it would have been injected at the bottom as well. What's wrong with it?
Mr. Smiley
You shouldn't inject UpdateValue(). I think that's what is causing the problem. How does the source code look (take a look with Firebug or something similar) like if you inject it as I suggested?
Tchami
Hi Tchami, I finally solved my problem. The registered function need a unique name and it must be registered to the Timer control. That's all! Thanks for your time.
Mr. Smiley
No problem, glad you solved it. If you have time, you might want to update your question with the answer so other people may benefit from it.
Tchami
A: 

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 
   { }
   }
Mr. Smiley