Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have tried two different ways to call a JavaScript function each of which works in other applications. Does it not like the Timer1_Tick or is it covered up by the Image1?
Neither of the "ClientScript.RegisterStartupScript" are working.

How do I make "ClientScript.RegisterStartupScript" start my JavaScript program: setProgress?

Default.aspx.cs
C#
protected void Timer1_Tick(object sender, EventArgs e)
{
    ...
    Hidden1.Value = Progress.ToString();
    ClientScript.RegisterStartupScript(GetType(), "myscript", "alert('Msg from server');", true);
    ClientScript.RegisterStartupScript(GetType(), "Javascript", "javaScript:showprogress();", true);
}


Default.aspx
ASP.NET
<script type="text/javascript">
        function showprogress() {
            alert("showprogress");
            set = document.getElementbyId("#<%= Hidden1.ClientID %>");
            document.getElementbyId("#<%= TextBox2.ClientID %>") = set;
        }
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            <ContentTemplate>
                <center><table><tr><td><asp:Timer ID="Timer1" Interval="5000" OnTick="Timer1_Tick" runat="server"></asp:Timer>
                <asp:Image ID="Image1" ImageUrl="~/pictures/1024 Mercury Mosaic.jpg" Width="800" Height="800" BorderWidth="5px"  runat="server" BackColor="#FFFFCC"></asp:Image></td></tr><tr><td align="center">
                <asp:TextBox ID="TextBox1" Text="0" Width="10" runat="server"></asp:TextBox></td></tr></table></center>            </ContentTemplate>
        </asp:UpdatePanel>
    <center><table><tr><td align="center"><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr></table></center>
    <input id="Hidden1" value="0" type="hidden" runat="server" />
Posted
Updated 9-Oct-14 16:27pm
v2
Comments
VC.J 10-Oct-14 1:00am    
yes javascript function is not called by your code I tried this RegisterClientScriptBlock but it also not working
you need to try some other approach
Some are in this link
http://forums.asp.net/t/1701997.aspx?How+to+call+a+Javascript+function+in+timer+Tick+event

Hey, Just try this while your PostBack Button outside asp:UpdatePanel
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:showprogress();", true); 



If the PostBack button inside UpdatePanel You MUST use ID of current UpdatePanel instead "this"

ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "script", "showprogress();", true);


If you are using ScriptManager.RegisterClientScriptBlock inside a timer you should keep some delay between Timer_Tick() a minimum of Interval="1000"


Note : Test with button click then change to Timer_Tick()
 
Share this answer
 
v2
Comments
Teledextri 10-Oct-14 14:10pm    
Error code:
JavaScript runtime error: 'showprogress' is undefined
My solution would be: do not use timers for calling javascript functions.

There are perfectly functional timing functions in javascript. Click here[^]. Try setTimeout(your-function, your-time-interval);

Call it in window.load or jQuery.ready event function.

The reason: mostly initiating round trip to the server is not a good idea for something that is client side code. Even in update panel which hides the post back from the user, the page in the background goes through (almost) full life-cycle which means it takes server time and resources...FOR EACH CONNECTED CLIENT. Bad idea.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900