ASP.NET AJAX Timer Trouble? Location is key.

If you’ve made much use of the ASP.NET AJAX Timer control, you may have noticed that it can behave somewhat unexpectedly.  In this post, I’m going to take a closer look at how the Timer works and the most significant factor that influences it:  Location.

Where the timer is placed on the page actually varies how it operates.  As a Timer’s delay interval approaches the processing time of its resulting PostBack, the difference that the Timer’s location makes becomes very significant.

Warning:  Timers and UpdatePanels are a potentially dangerous combination.  For long intervals, the simplicity can often be worth the performance trade-off.  However, as your interval becomes smaller, you should very seriously consider a lighter weight approach to polling for data updates.

That said, I know that you’re probably still going to be using them even when you shouldn’t.  So, let’s take a closer look at how the Timer actually works.

The Timer control’s underlying mechanism

It’s important to understand that the Timer control is simply an elaborate abstraction for combining JavaScript’s setTimeout and ASP.NET’s __doPostBack. 

Assuming that UpdatePanel1′s OnLoad event was handled in the same way that the Timer1′s OnTick was handled, a Timer declared like this:

<asp:Timer runat="server" id="Timer1" Interval="5000" />

Could be very roughly approximated by JavaScript such as:

function pageLoad() {window.setTimeout("__doPostBack('UpdatePanel1', '')", 5000);
}

There’s nothing magic about the Timer control.  It attempts to approximate the Timer control that we’re used to in WinForms development, but is still subject to the limitations of the stateless HTTP protocol.

Keeping this in mind, let’s look at two cases.  In both, the Timer will have an Interval of five seconds and will trigger a partial postback taking four seconds to complete.  However, due to Timer placement, the actual time between updates in these two cases will differ by around 80%.

Case 1:  A Timer inside UpdatePanel content

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><ContentTemplate><asp:Timer runat="server" ID="Timer1" OnTick="Timer1_Tick" Interval="5000" /><asp:Literal runat="server" ID="Literal1" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{System.Threading.Thread.Sleep(4000);Literal1.Text = DateTime.Now.ToLongTimeString();
}

This is probably the easiest way of implementing a Timer.  You can just drop it in the UpdatePanel, create an OnTick handler, and not worry about setting up triggers or anything else.

However, even though the Timer interval is very clearly specified as five seconds, this UpdatePanel will actually take just over nine seconds for each Tick to fire.  The reason for this large discrepancy is the location of the Timer.  Take a look at the partial postback response, and it becomes clear why:

Timer response when contained in an UpdatePanel

Because the Timer is inside the UpdatePanel’s ContentTemplate, it will be reinitialized every time the UpdatePanel refreshes.  It actually comes within one second of triggering another OnTick event, but is reset by the partial postback’s return.

This may or may not be desirable in a given scenario, but can be fairly unexpected.

Case 2:  A Timer outside UpdatePanel content

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:Timer runat="server" ID="Timer1" Interval="5000" OnTick="Timer1_Tick" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><Triggers><asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /></Triggers><ContentTemplate><asp:Literal runat="server" ID="Literal1" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{System.Threading.Thread.Sleep(4000);Literal1.Text = DateTime.Now.ToLongTimeString();
}

Implementing the Timer this way removes the reinitialization interference, but exposes another potential problem.  Since the Timer ticks are now fixed at an absolute five second interval, only one second less than our partial postback requires to complete, the UpdatePanel spends nearly all of its time in async postback.

Depending on your application this may be exactly what you want, but it also might be very detrimental to the usability and performance of the application.  For example, if you were to locate a one second Timer outside an UpdatePanel that took two seconds to refresh, it would never manage to complete a single update.

Conclusion

As you can see, Timer placement can significantly and unintuitively affect how the Timer actually operates.  Understanding all of the influencing factors puts you in control and enables you to arrange your Timers to best suit your application.

I’m going to leave you with a bit of a pop-quiz.  I hope you were paying attention!  Consider the following code sample, from a question posted on the asp.net forums:

<asp:ScriptManager runat="server" id="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><ContentTemplate><asp:Literal runat="server" ID="Literal1" /><asp:Timer runat="server" ID="Timer1" Interval="5000" OnTick="Timer1_Tick" /></ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="UpdatePanel2"><ContentTemplate><asp:Literal runat="server" ID="Literal2" /><asp:Timer runat="server" ID="Timer2"Interval="10000" OnTick="Timer2_Tick" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{Literal1.Text = DateTime.Now.ToString();
}protected void Timer2_Tick(object sender, EventArgs e)
{Literal2.Text = DateTime.Now.ToString();
}

Roughly how often do you think I should expect to see Literal2′s timestamp update?  Leave a comment if you know the answer. 

Additionally, this code can be “fixed” to yield more intuitive results by adding only one property to one control.  What would you do?

转载于:https://www.cnblogs.com/happy-Chen/p/3688270.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399515.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linq 分组求和的一般方法

//var query from d in expenseApplyModel.ApplyBillList.AsEnumerable() // group d by d.ExpenseItemID into g // select new // { // ExpenseItemID g.Key, // ExpenseAmount g.Sum(t > t.ExpenseAmount) // };//分组求和新添加的费用项目 var query expenseApplyMo…

在屏幕上打印杨辉三角

这就是杨辉三角&#xff0c;也叫贾宪三角。这于我们现在的学习联系最紧密的是2项式乘方展开式的系数规律。如图&#xff0c;在贾宪三角中&#xff0c;第3行的第三个数恰好对应着两数和的平方公式依次下去。 杨辉三角是一个由数字排列成的三角形数表&#xff0c;一般形式如下&am…

jquery 获取 A 标签 超级链接属性

var icon_nav_href1 $("#indexiconnavid>li:nth-child(1) a").attr("href"); //抓取当前url

apache ab 测试 apr_socket_connect(): 由于目标机器积极拒绝 无法连接

遇到这种情况一般是你开的并行数量太多了。。。例如:ab -c 1000 -n 10000 http://localhost/index.html 如此大的请求就会挂掉&#xff0c;不过还是有补救措施的&#xff0c;可以通过增加并发数上限解决这个问题&#xff0c;步骤如下&#xff1a; 1、停止Apache服务&#xff1b…

maven 结合idea入门

reference : http://www.cnblogs.com/ramantic/p/7735323.html转载于:https://www.cnblogs.com/tangchangcai/p/7735690.html

ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)

1 imageView.setScaleType(ImageView.ScaleType.FIT_XY ); 1 这里我们重点理解ImageView的属性android:scaleType&#xff0c;即ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / an…

CSS如何让DIV的宽度随内容的变化

让div根据内容改变大小 div{ width:auto; display:inline-block !important; display:inline; } https://www.cnblogs.com/limeiky/p/6289307.html

第一幕 基础起步

1、选择版本python2 or python3 话不多说 python3 2、不同操作系统下的安装配置python 2.1、Linux系统一般默认安装有python&#xff0c;打开终端窗口检查是否安装python&#xff0c;在终端输入python --version 或 python3 --version&#xff0c;如果有则会显示python版本&am…

footer置底的几种方式

/* 通过calc()函数让内容区块自动伸缩 */ .my-body{min-height: calc(71.7vh - 80px); } footer{height:50px; } 参考 :https://www.jianshu.com/p/6efe2c76a2dd

JAVA流程控制学习总结

1、块作用域语句&#xff08;复合语句&#xff09;即用一对花括号将若干语句括起来&#xff0c;目的是从语法上将多条语句解释为一条语句。这里要注意一点&#xff0c;java语言中块作用域语句可以嵌套&#xff0c;但不可以在嵌套的两层中声明同名的变量。举个错误的例子&#x…

如何在MATLAB下把模糊推理系统转化为查询表(转载)

如何在MATLAB下把模糊推理系统转化为查询表(原创) http://foundy.blog.163.com/blog/static/2633834420090212202156/?modeedit 在SIMULINK里把模糊逻辑生成查寻表&#xff08;原创&#xff09; http://foundy.blog.163.com/blog/static/2633834420100150439615/ 转载于:ht…

对SPA(单页面应用)的总结

目录 1、单页面应用&#xff08;SPA&#xff09;的概念&#xff1a;2、作用&#xff08;好处&#xff09;3、缺点4、实现SPA1、单页面应用&#xff08;SPA&#xff09;的概念&#xff1a; 1、single-page application是一种特殊的Web应用。它将所有的活动局限于一个Web页面中&a…

Linux运维:现状、入门和未来之路

今天想谈谈“运维”这一行&#xff0c;我将从以下五个部分来和大家分析一下目前Linux这个行业的现状以及如何学好Linux、成为专业运维人员和云服务对运维的影响。一、linux行业现状我们知道Linux诞生于1991年&#xff0c;它在国外的发展是非常迅速的&#xff0c;即使目前国内越…

wordpress 怎么获取站点标题

<?php $blog_title get_bloginfo(name); ?> 参考:https://zhidao.baidu.com/question/585555467971876845.html

CentOS安装Chrome

问题 在CentOS安装Chrome会遇到 libstdc.so.6(GLIBCXX_3.4.15)(64bit) 依赖失败的问题, 即使下载了最新的libstdc.so.6(包含GLIBCXX_3.4.15)也解决不了问题. Resolving Dependencies --> Running transaction check ---> Package google-chrome-beta.x86_64 0:35.0.1916.…

org.apache.hadoop.ipc.Client: Retrying connect to server

这个问题导致jps查看结点进程时发现找不到NodeManager或一段时间后消失&#xff0c;网上查找了很多博客&#xff0c;因hadoop版本不一样且出错的原因也可能不同&#xff0c;所以找了老半天。 步骤&#xff1a;jps --> 看logs中.log文件 --> 百度。 最后找到一个版本的博客…

转: RSA原理 阮一峰的博客

转&#xff1a;http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html 讲的非常细致&#xff0c;易懂。

wordpress后台外观没有菜单和小工具的解决方法

进入wordpress后台&#xff0c;打开functions.php这个模板 在里面加入这段代码&#xff1a; if ( function_exists(register_sidebar) )register_sidebar(array(before_widget > <div class"sidebox"> ,after_widget > </div>,before_title >…

C#格式化字符串中转义大括号“{}”

C#格式化字符串中转义大括号“{}” 原文:C#格式化字符串中转义大括号“{}”今天&#xff0c;用C#写程序操作Excel&#xff0c;读取单元格内容根据所需格式生成字符串&#xff0c;使用String.Format(string format,object arg0)方法。以前只知“{0}”为索引占位符(即格式项)&…

JavaScript--数据结构与算法之二叉树

树是一种非线性的数据结构&#xff0c;以分层的方式存储数据。 二叉树&#xff1a;查找非常快&#xff0c;而且二叉树添加或者删除元素也非常快。 形象的可以描述为组织结构图&#xff0c;用来描述一个组织的结构。树是由边连接的点组成。树的一些基本概念&#xff1a; …