计时器滴答时代码重新加载 [英] Code reloads when timer ticks

查看:61
本文介绍了计时器滴答时代码重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在内部和updatepanel内正确打勾的Timer。出于某种原因,它也会再次调用Page_Load?更新面板中唯一的内容是脚本管理器和计时器。



Hi, I have a Timer that does tick correctly inside of and updatepanel. For some reason, it calls Page_Load again too? The only things inside the update panel is a script manager and timer.

<pre> <asp:UpdatePanel runat="server" ID="upTimer">
                    <ContentTemplate>
                        <asp:scriptmanager runat="server" EnableCdn="True" LoadScriptsBeforeUI="False"></asp:scriptmanager>
<asp:timer runat="server" ID="MainTimer" Enabled="False" Interval="1000" OnTick="MainTimer_Tick"></asp:timer>
     </ContentTemplate>
</asp:UpdatePanel>





我不知道为什么会这样。代码完全被刷新。它在方法中生成随机字符串,然后启动计时器。计时器接受字符串并将其放在标签上,该方法不在计时器中,也不再被调用。随机字符串的代码位于Page_Load中。请尽可能帮助我!



编辑:发现该页面仍在重新加载,即使它在更新面板中,现在无论如何我可以阻止它?



我尝试了什么:



一切。我真的不知道该尝试什么。我四处搜索,找不到其他人这个问题。



I got no idea why this is happening. The code is completely being refreshed. It generates a random string in a method, then starts the timer. The timer takes the string and puts it on the label, the method is not in the timer and is not being called again. The code for the random string is located in Page_Load. Please help me if possible!

Found out that page still reloads even though its in a update panel, now anyway I can stop that?

What I have tried:

Everything. I seriously don't know what to tried. I've searched all around and couldn't find anyone else with this issue.

推荐答案

这就是updatepanels \ asynch操作的工作方式......它构建了整个页面(即调用page_load等)然后计算出哪些位要发送回浏览器。它的易用性而不是效率。



您需要做的是重新安排您的代码以便它可以工作。将执行该功能的代码放入函数中,然后仅在不是回发的情况下在Page_Load中调用该函数;



This is just how updatepanels\asynch operations work...it build the entire page (ie calls page_load etc) then works out which bits to send back to the browser. It's built for its ease of use rather than its efficiency.

What you have to do is re-arrange your code so it works. Put the code that does the work in a function then call that function in Page_Load only if it isn't a postback;

if (!Page.IsPostback)
{
    DoWork();
}





现在也可以在MainTimer_Tick活动中调用DoWork。这将确保代码在页面首次加载时运行,然后每次定时器触发时运行。



Now also call DoWork inside your MainTimer_Tick event. That will ensure the code is ran when the page first loads, and then every time the timer fires.


我检查它是否不是
ScriptManager1.IsInAsyncPostBack

如果是,我会恢复存储在数据库中的变量。

and if it was I would restore the variables, which I stored in the database.


这篇关于计时器滴答时代码重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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