Link to home
Start Free TrialLog in
Avatar of Relegence
RelegenceFlag for Israel

asked on

Calling a javascript function from server side, using AJAX timer

Hello,

I am developing a ASP.NET ajax application.
I am using a timer:

<asp:Timer ID="Timer1" runat="server" OnTick="Timer_Tick" Interval="5000" />      

The only thing I want to do is to call a javascript function on every tick of the timer:

 protected void Timer_Tick(object sender, EventArgs e)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "blink", "<script>blinkit('2');</script>");
        }

This doesn't work.
The javascript function is being called only if I put the 'RegisterClientScriptBlock' in the Page_Load.

How can I call the javascript every couple of seconds?

Thank you
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Hi,

Don't know about the rest of your code but if you try using:

protected void Timer_Tick(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "blink", "blinkit('2');", true);
        }

it should work depending if you are making a full postback or not (which is needed for this way to work). If You are controlling the contents of an update panel with the timer I guess you need to add the timer as a postback trigger (not async) for the update panel.

/Carl.
Avatar of Relegence

ASKER

I am afraid it didn't help.

Below is my code
<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" OnTick="Timer_Tick" Interval="5000" />                
            </ContentTemplate>
        </asp:UpdatePanel>  
        <asp:Label ID="lbl" runat="server"></asp:Label>
        <label id="lblHTML">test</label>
    </div>
    </form>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot. it works perfectly!!