如何制作“ClientScript.RegisterStartupScript”启动我的JavaScript程序:setProgress? [英] How do I make "ClientScript.RegisterStartupScript" start my JavaScript program: setProgress?

查看:75
本文介绍了如何制作“ClientScript.RegisterStartupScript”启动我的JavaScript程序:setProgress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种不同的方法来调用JavaScript函数,每个函数都可以在其他应用程序中运行。它不喜欢Timer1_Tick还是被Image1覆盖?

两者都没有ClientScript.RegisterStartupScript工作。



如何让ClientScript.RegisterStartupScript启动我的JavaScript程序:setProgress?



Default.aspx.cs

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

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



Default.aspx

<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" />

推荐答案

嘿,Just在你的PostBack按钮外面尝试这个asp:UpdatePanel

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







如果UpdatePanel中的PostBack按钮必须使用当前UpdatePanel的ID而不是this






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

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





如果您在计时器内使用ScriptManager.RegisterClientScriptBlock,您应该在Timer_Tick()之间保持一定的延迟至少Interval =1000





注意:点击按钮进行测试然后更改为Timer_Tick()



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()


我的解决方案是:不要使用计时器调用javascript函数。



javascript中有完美的函数计时函数。 点击此处 [ ^ ]。尝试setTimeout(你的函数,你的时间间隔);



在window.load或jQuery.ready事件函数中调用它。



原因:主要是开始往返服务器对于客户端代码的东西不是一个好主意。即使在从用户隐藏帖子的更新面板中,后台中的页面也会经历(几乎)完整的生命周期,这意味着它需要服务器时间和资源...... 对于每个连接的客户端。不好主意。
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.


这篇关于如何制作“ClientScript.RegisterStartupScript”启动我的JavaScript程序:setProgress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆